Get all types implementing specific open generic type

后端 未结 4 1976
滥情空心
滥情空心 2020-12-01 10:05

How do I get all types that implementing a specific open generic type?

For instance:

public interface IUserRepository : IRepository
         


        
4条回答
  •  南笙
    南笙 (楼主)
    2020-12-01 10:50

    You could try

    openGenericType.IsAssignableFrom(myType.GetGenericTypeDefinition()) 
    

    or

    myType.GetInterfaces().Any(i => i.GetGenericTypeDefinition() = openGenericType)
    

提交回复
热议问题