zoukankan      html  css  js  c++  java
  • C#使用反射特性构建访问者模式

    代码出自《c#3.0设计模式》

    两个结构的对象

        class Element
        {
            public Element Next { get; set; }
            public Element Part { get; set; }
            public Element() { }
            public Element(Element next)
            {
                Next = next;
            }
        }
        class ElementWithLink : Element
        {
            public ElementWithLink(Element part, Element next)
            {
                Next = next;
                Part = part;
            }
        }



    与反射相关的核心代码

        abstract class IVisitor
        {
            public void ReflectiveVisit(Element element)
            {
                Type[] types = new Type[]{element.GetType()};
                //搜索参数与指定参数类型匹配的指定公共方法
                //第一个参数是方法名称
                //第二个参数为参数对象的数组,顺序和类型必须一致
                MethodInfo methodinfo = this.GetType().GetMethod("Visit", types);
                if (methodinfo != null)
                {
                    //使用指定的参数调用当前实例所表示的方法或构造函数
                    //第一个参数是被调用方法的对象的实例
                    //第二个参数是该方法的参数,顺序和类型都必须一致
                    methodinfo.Invoke(this, new object[] { element });
                }
                else
                {
                    Console.WriteLine("Unexpected Visit");
                }
            }
        }


    如果不懂请看注释

    访问器

        class CountVisitor : IVisitor
        {
            public int Count { get; set; }
            public void CountElements(Element element)
            {
                ReflectiveVisit(element);
                if (element.Part != null)
                {
                    CountElements(element.Part);
                }
                if (element.Next != null)
                {
                    CountElements(element.Next);
                }
            }
            public void Visit(ElementWithLink element)
            {
                Console.WriteLine("not counting");
            }
            public void Visit(Element element)
            {
                Count++;
            }
        }



    客户端代码

        class Program
        {
            static void Main(string[] args)
            {
                Element objectStructure = new Element(
                                            new Element(
                                                new ElementWithLink(
                                                    new Element(
                                                        new Element(
                                                            new ElementWithLink(
                                                                new Element(null),new Element(null)
                                                            ))),
                                                            new Element(
                                                                new Element(
                                                                    new Element(null)
                                                                    )))));
                Console.WriteLine("count it");
                CountVisitor visitor = new CountVisitor();
                visitor.CountElements(objectStructure);
                Console.WriteLine(visitor.Count);
                Console.ReadKey();
            }
        }


    关于访问者模式    反射的内容
    以后肯定要更详细的介绍

  • 相关阅读:
    javaScript中eval()方法转换json对象
    JS 根据参数是否为空进行true|false判断呢
    mybatis返回map类型数据空值字段不显示(三种解决方法)
    值类型与引用类型的区别
    随机生成四位数字和字母
    彩票
    冒泡排序
    查询资料:二分查找法
    随机数生成机制
    运算符
  • 原文地址:https://www.cnblogs.com/liulun/p/1538875.html
Copyright © 2011-2022 走看看