C# 通过反射实现接口
如何通过C#中的反射获得一个接口的所有实现?您指的是a类型实现的所有接口吗 像这样:C# 通过反射实现接口,c#,reflection,interface,C#,Reflection,Interface,如何通过C#中的反射获得一个接口的所有实现?您指的是a类型实现的所有接口吗 像这样: ObjX foo = new ObjX(); Type tFoo = foo.GetType(); Type[] tFooInterfaces = tFoo.GetInterfaces(); foreach(Type tInterface in tFooInterfaces) { // do something with it } 希望对您有所帮助。请查看Assembly.GetTypes()方法。它返回
ObjX foo = new ObjX();
Type tFoo = foo.GetType();
Type[] tFooInterfaces = tFoo.GetInterfaces();
foreach(Type tInterface in tFooInterfaces)
{
// do something with it
}
希望对您有所帮助。请查看
Assembly.GetTypes()
方法。它返回可在程序集中找到的所有类型。您所要做的就是遍历每个返回的类型,并检查它是否实现了必要的接口
一种方法是使用Type.IsAssignableFrom
方法
这是一个例子myInterface
是您正在搜索的接口和实现
Assembly myAssembly;
Type myInterface;
foreach (Type type in myAssembly.GetTypes())
{
if (myInterface.IsAssignableFrom(type))
Console.WriteLine(type.FullName);
}
我确实相信这不是解决问题的一种非常有效的方法,但至少,这是一个很好的起点。Assembly=Assembly.getExecutionGassembly();
Assembly assembly = Assembly.GetExecutingAssembly();
List<Type> types = assembly.GetTypes();
List<Type> childTypes = new List<Type>();
foreach (Type type in Types) {
foreach (Type interfaceType in type.GetInterfaces()) {
if (interfaceType.Equals(typeof([yourinterfacetype)) {
childTypes.Add(type)
break;
}
}
}
列表类型=assembly.GetTypes();
List childTypes=new List();
foreach(类型中的类型){
foreach(Type.GetInterfaces()中的类型interfaceType){
if(interfaceType.Equals(typeof([yourinterfacetype)){
添加(类型)
打破
}
}
}
可能是这样的……答案是这样的;它搜索整个应用程序域——也就是说,应用程序当前加载的每个程序集
/// <summary>
/// Returns all types in the current AppDomain implementing the interface or inheriting the type.
/// </summary>
public static IEnumerable<Type> TypesImplementingInterface(Type desiredType)
{
return AppDomain
.CurrentDomain
.GetAssemblies()
.SelectMany(assembly => assembly.GetTypes())
.Where(type => desiredType.IsAssignableFrom(type));
}
您可能还希望此函数查找实际的具体类型,即筛选出摘要、接口和泛型类型定义
public static bool IsRealClass(Type testType)
{
return testType.IsAbstract == false
&& testType.IsGenericTypeDefinition == false
&& testType.IsInterface == false;
}
您必须循环所有感兴趣的程序集。从该程序集可以获得它定义的所有类型。请注意,当您执行AppDomain.CurrentDomain.Assemblys时,您只能获得加载的程序集。程序集在需要时才会加载,这意味着您必须在加载之前显式加载程序集开始搜索。以下是一些扩展方法,如所建议的,这些方法可能对此有用。此代码只是对已接受答案的重新构造 代码 要获取当前AppDomain中的实现者,请执行以下操作:
var appDomainAssemblies = AppDomain.CurrentDomain.GetAssemblies();
var httpModules = typeof(IHttpModule).GetImplementors(appDomainAssemblies);
这在实现上述接口的其他程序集中找不到类型。请注意,这段代码看不到未来——它在尚未加载的接口中找不到此接口的实现。我想我可能会将其转换为类型的扩展方法,可能会非常有用。Steve:你能解释一下类似的情况吗r我遇到的问题:您可以将
IsRealClass
简化为return!(testType.isastract | | | | testType.IsGenericTypeDefinition | | | | testType.IsInterface)
。SelectMany(assembly=>assembly.GetTypes())这行给了我以下异常:{“无法加载文件或程序集'Microsoft.WindowsAzure.MobileServices.Management,版本=1.0.0,区域性=中性,PublicKeyToken=f300afd708cefcd3'或其依赖项之一。系统找不到指定的文件。”:“Microsoft.WindowsAzure.MobileServices.Management,版本=1.0.0,区域性=中性,PublicKeyToken=f300afd708cefcd3”}相关问题:
/// <summary>
/// Returns all types in <paramref name="assembliesToSearch"/> that directly or indirectly implement or inherit from the given type.
/// </summary>
public static IEnumerable<Type> GetImplementors(this Type abstractType, params Assembly[] assembliesToSearch)
{
var typesInAssemblies = assembliesToSearch.SelectMany(assembly => assembly.GetTypes());
return typesInAssemblies.Where(abstractType.IsAssignableFrom);
}
/// <summary>
/// Returns the results of <see cref="GetImplementors"/> that match <see cref="IsInstantiable"/>.
/// </summary>
public static IEnumerable<Type> GetInstantiableImplementors(this Type abstractType, params Assembly[] assembliesToSearch)
{
var implementors = abstractType.GetImplementors(assembliesToSearch);
return implementors.Where(IsInstantiable);
}
/// <summary>
/// Determines whether <paramref name="type"/> is a concrete, non-open-generic type.
/// </summary>
public static bool IsInstantiable(this Type type)
{
return !(type.IsAbstract || type.IsGenericTypeDefinition || type.IsInterface);
}
var callingAssembly = Assembly.GetCallingAssembly();
var httpModules = typeof(IHttpModule).GetInstantiableImplementors(callingAssembly);
var appDomainAssemblies = AppDomain.CurrentDomain.GetAssemblies();
var httpModules = typeof(IHttpModule).GetImplementors(appDomainAssemblies);