下面的代码通过反射获取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);