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();
        }
    }


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

posted @ 2009-08-04 19:01  liulun  阅读(517)  评论(0编辑  收藏  举报