• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

C#使用反射特性构建访问者模式

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

代码出自《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();
        }
    }


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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#static的全部用法收集整理发布时间:2022-07-13
下一篇:
C# BackgroundWorker 示范:       C#backgroundWorker用 ...发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap