C# 如何找到实现给定接口的所有类?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/699852/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to find all the classes which implement a given interface?
提问by Martin
Under a given namespace, I have a set of classes which implement an interface. Let's call it ISomething
. I have another class (let's call it CClass
) which knows about ISomething
but doesn't know about the classes which implement that interface.
在给定的命名空间下,我有一组实现接口的类。让我们称之为ISomething
。我有另一个类(我们称之为CClass
),它知道ISomething
但不知道实现该接口的类。
I would like that CClass
to look for all the implementation of ISomething
, instantiate an instance of it and execute the method.
我希望CClass
查找 的所有实现ISomething
,实例化它的一个实例并执行该方法。
Does anybody have an idea on how to do that with C# 3.5?
有人知道如何使用 C# 3.5 做到这一点吗?
采纳答案by Matt Hamilton
A working code-sample:
一个工作代码示例:
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
}
EditAdded a check for a parameterless constructor so that the call to CreateInstance will succeed.
编辑添加了对无参数构造函数的检查,以便对 CreateInstance 的调用成功。
回答by CMS
A example using Linq:
使用 Linq 的示例:
var types =
myAssembly.GetTypes()
.Where(m => m.IsClass && m.GetInterface("IMyInterface") != null);
回答by Quintin Robinson
You could use something like the following and tailor it to your needs.
您可以使用以下内容并根据您的需要进行调整。
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();
}
}
This code could use some performance enhancements but it's a start.
这段代码可以使用一些性能增强,但这是一个开始。
回答by Mitch Denny
You can get a list of loaded assemblies by using this:
您可以使用以下命令获取已加载程序集的列表:
Assembly assembly = System.Reflection.AppDomain.CurrentDomain.GetAssemblies()
From there, you can get a list of types in the assembly (assuming public types):
从那里,您可以获得程序集中的类型列表(假设为公共类型):
Type[] types = assembly.GetExportedTypes();
Then you can ask each type whether it supports that interface by finding that interface on the object:
然后您可以通过在对象上查找该接口来询问每种类型是否支持该接口:
Type interfaceType = type.GetInterface("ISomething");
Not sure if there is a more efficient way of doing this with reflection.
不确定是否有更有效的反射方式来做到这一点。
回答by Kiran
foreach (Type t in Assembly.GetCallingAssembly().GetTypes())
{
if (t.GetInterface("ITheInterface") != null)
{
ITheInterface executor = Activator.CreateInstance(t) as ITheInterface;
executor.PerformSomething();
}
}
回答by bane 975
Maybe we should go this way
也许我们应该走这条路
foreach ( var instance in Assembly.GetExecutingAssembly().GetTypes().Where(a => a.GetConstructor(Type.EmptyTypes) != null).Select(Activator.CreateInstance).OfType<ISomething>() )
instance.Execute();