【发布时间】:2010-10-16 12:54:13
【问题描述】:
在给定的命名空间下,我有一组实现接口的类。我们称之为ISomething。我有另一个类(我们称之为CClass),它知道ISomething,但不知道实现该接口的类。
我希望CClass 查找ISomething 的所有实现,实例化它的一个实例并执行该方法。
有人知道如何使用 C# 3.5 做到这一点吗?
【问题讨论】:
标签: c# reflection
在给定的命名空间下,我有一组实现接口的类。我们称之为ISomething。我有另一个类(我们称之为CClass),它知道ISomething,但不知道实现该接口的类。
我希望CClass 查找ISomething 的所有实现,实例化它的一个实例并执行该方法。
有人知道如何使用 C# 3.5 做到这一点吗?
【问题讨论】:
标签: c# reflection
工作代码示例:
var instances = from t in Assembly.GetExecutingAssembly().GetTypes()
where t.GetInterfaces().Contains(typeof(ISomething))
&& t.GetConstructor(Type.EmptyTypes) != null
select Activator.CreateInstance(t) as ISomething;
foreach (var instance in instances)
{
instance.Foo(); // where Foo is a method of ISomething
}
编辑添加了对无参数构造函数的检查,以便对 CreateInstance 的调用成功。
【讨论】:
您可以使用以下方法获取已加载程序集的列表:
Assembly assembly = System.Reflection.AppDomain.CurrentDomain.GetAssemblies()
从那里,您可以获得程序集中的类型列表(假设是公共类型):
Type[] types = assembly.GetExportedTypes();
然后你可以通过在对象上查找该接口来询问每种类型是否支持该接口:
Type interfaceType = type.GetInterface("ISomething");
不确定是否有更有效的反射方式。
【讨论】:
使用 Linq 的示例:
var types =
myAssembly.GetTypes()
.Where(m => m.IsClass && m.GetInterface("IMyInterface") != null);
【讨论】:
foreach (Type t in Assembly.GetCallingAssembly().GetTypes())
{
if (t.GetInterface("ITheInterface") != null)
{
ITheInterface executor = Activator.CreateInstance(t) as ITheInterface;
executor.PerformSomething();
}
}
【讨论】:
您可以使用以下内容并根据您的需要进行调整。
var _interfaceType = typeof(ISomething);
var currentAssembly = System.Reflection.Assembly.GetExecutingAssembly();
var types = GetType().GetNestedTypes();
foreach (var type in types)
{
if (_interfaceType.IsAssignableFrom(type) && type.IsPublic && !type.IsInterface)
{
ISomething something = (ISomething)currentAssembly.CreateInstance(type.FullName, false);
something.TheMethod();
}
}
此代码可以使用一些性能增强功能,但这只是一个开始。
【讨论】:
也许我们应该走这条路
foreach ( var instance in Assembly.GetExecutingAssembly().GetTypes().Where(a => a.GetConstructor(Type.EmptyTypes) != null).Select(Activator.CreateInstance).OfType<ISomething>() )
instance.Execute();
【讨论】: