[英]How to get all the static classes from all the loaded assemblies in an app-domain and invoke a static method using reflection
my requirement is as follows, is this possible ? 我的要求如下,这可能吗? if yes can someone please point me to any resources on this ?
如果是的话,有人可以指出我的任何资源吗?
Try this: 尝试这个:
foreach(Assembly asm in AppDomain.CurrentDomain.GetAssemblies())
{
if (asm.GetName().Name.EndsWith("static"))
{
foreach(Type type in asm.GetTypes())
{
if (type.Name.EndsWith("cache"))
{
MethodInfo method = type.GetMethod("invalidate", BindingFlags.Static | BindingFlags.Public, null, Type.EmptyTypes, null);
if (method != null)
method.Invoke(null, null);
}
}
}
}
Or... if you prefer LINQ: 或者...如果您更喜欢LINQ:
foreach(MethodInfo method in
AppDomain.CurrentDomain
.GetAssemblies().Where(asm => asm.GetName().Name.EndsWith("static"))
.SelectMany(asm => asm.GetTypes().Where(type => type.Name.EndsWith("cache"))
.Select(type => type.GetMethod("invalidate", BindingFlags.Static | BindingFlags.Public, null, Type.EmptyTypes, null)).Where(method => method != null)))
method.Invoke(null, null);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.