C# 如果类有一个接口;实施;另一个接口

C# 如果类有一个接口;实施;另一个接口,c#,reflection,C#,Reflection,我目前正在努力实现以下目标: 我有一个接口这个接口: public interface IRepository<TEntity> { //Some Methods } 我试图实现的是:提供类型AttractionRepository,我想搜索它的接口,并获得哪个接口在扩展Interface IRepository 我的代码如下: Type[] interfaces = typeof(AttractionRepository).GetInterfaces(); //I get

我目前正在努力实现以下目标:

我有一个接口这个接口:

public interface IRepository<TEntity>
{
   //Some Methods
}
我试图实现的是:提供类型AttractionRepository,我想搜索它的接口,并获得哪个接口在扩展Interface IRepository

我的代码如下:

Type[] interfaces = typeof(AttractionRepository).GetInterfaces(); //I get three interfaces here, which is fine.
Type iface = null;

foreach (Type t in interfaces) //Iterate through all interfaces
    foreach(Type t1 in t.GetInterfaces()) //For each of the interfaces an interface is extending, I want to know if there's any interface that is "IRepository<Attraction>"
        if (t1.IsSubclassOf(typeof(IRepository<>).MakeGenericType(typeof(Attraction)))) //Always false
            iface = t;
Type[]interfaces=typeof(AttractionRepository).GetInterfaces()//我这里有三个接口,这很好。
类型iface=null;
foreach(接口中的类型t)//遍历所有接口
foreach(t.GetInterfaces()中的t1类型)//对于接口正在扩展的每个接口,我想知道是否有任何接口是“IRepository”
if(t1.IsSubclassOf(typeof(IRepository).MakeGenericType(typeof(Attraction)))//始终为false
iface=t;

我尝试过其他几种解决方案,但没有成功。

类似的解决方案非常适合这种情况:

/// <summary>
/// Returns whether or not the specified class or interface type implements the specified interface.
/// </summary>
/// <param name="implementor">The class or interface that might implement the interface.</param>
/// <param name="interfaceType">The interface to look for.</param>
/// <returns><b>true</b> if the interface is supported, <b>false</b> if it is not.</returns>
public static bool ImplementsInterface(this Type implementor, Type interfaceType)
{
    if (interfaceType.IsGenericTypeDefinition)
    {
        return (implementor.IsGenericType && implementor.GetGenericTypeDefinition() == interfaceType) ||
            (implementor.GetInterfaces().Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == interfaceType));
    }
    else return interfaceType.IsAssignableFrom(implementor);
}
//
///返回指定的类或接口类型是否实现指定的接口。
/// 
///可能实现接口的类或接口。
///要查找的接口。
///如果接口受支持,则为true;如果接口不受支持,则为false。
公共静态bool实现接口(此类型实现器,类型interfaceType)
{
if(interfaceType.IsGenericTypeDefinition)
{
返回(implementor.IsGenericType&&implementor.GetGenericTypeDefinition()==interfaceType)||
(implementor.GetInterfaces().Any(i=>i.IsGenericType&&i.GetGenericTypeDefinition()==interfaceType));
}
否则返回interfaceType.IsAssignableFrom(实现者);
}
问题是,由于您正在寻找的接口是一个通用接口,因此没有用于此功能的内置函数

在您的特定情况下,您可以使用如下方式:

Type implementingInterface = typeof(AttractionRepository).GetInterfaces().Where(i => i.ImplementsInterface(typeof(IRepository<>))).FirstOrDefault();
Type implementingInterface=typeof(AttractionRepository).GetInterfaces().Where(i=>i.ImplementsInterface(typeof(IRepository)).FirstOrDefault();

效果很好!如果我需要排除相同的接口呢?对于iInstance,如果我对IRepository进行测试,我希望它返回false,那么我需要做什么?我认为只要删除这部分:(implementor.IsGenericType&&implementor.GetGenericTypeDefinition()==interfaceType)| |就可以了。也许一个名为implementsButisNoterface()的替代扩展方法适合于此。
/// <summary>
/// Returns whether or not the specified class or interface type implements the specified interface.
/// </summary>
/// <param name="implementor">The class or interface that might implement the interface.</param>
/// <param name="interfaceType">The interface to look for.</param>
/// <returns><b>true</b> if the interface is supported, <b>false</b> if it is not.</returns>
public static bool ImplementsInterface(this Type implementor, Type interfaceType)
{
    if (interfaceType.IsGenericTypeDefinition)
    {
        return (implementor.IsGenericType && implementor.GetGenericTypeDefinition() == interfaceType) ||
            (implementor.GetInterfaces().Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == interfaceType));
    }
    else return interfaceType.IsAssignableFrom(implementor);
}
Type implementingInterface = typeof(AttractionRepository).GetInterfaces().Where(i => i.ImplementsInterface(typeof(IRepository<>))).FirstOrDefault();