C#学习 - LINQ使用

下面的代码通过反射获取Server类下所有方法,并将带有InterceptAttribute的方法及其Intercept属性保存在Dictionary中。

            Assembly ass = Assembly.GetAssembly(typeof(Server));
            Type t = ass.GetType("MyServer.Server");

            var a = (from v in t.GetMethods()
                     where (v.CustomAttributes.Count() > 0 &&
                     v.GetCustomAttribute<InterceptAttribute>() != null)
                     select 
                     new KeyValuePair<string, string>
                     (v.Name, v.GetCustomAttribute<InterceptAttribute>().CommandPattern));

            dict = a.ToDictionary(x => x.Key, x => x.Value);

猜你喜欢

转载自blog.csdn.net/jianhui_wang/article/details/80925827