反射以获取类是否具有“实现”另一个接口的接口

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


我有一个接口这个接口:


public interface IRepository<TEntity>

{

   //Some Methods

}

然后我有另一个接口扩展了上面的接口:


public interface IAttractionRepository : IRepository<Attraction>

{

   //More methods

}

最后,我有一个实现(也实现了其他接口):


public class AttractionRepository : ISomethingElse, IAnotherSomethingElse, IAttractionRepository

{

  //Implementations and methods

}

我想要实现的是:提供类型 AttractionRepository,我想搜索它的接口并获取扩展接口 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;

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


qq_遁去的一_1
浏览 97回答 1
1回答
打开App,查看更多内容
随时随地看视频慕课网APP