简体   繁体   English

获取类实现的泛型接口的类型参数

[英]Getting type arguments of generic interfaces that a class implements

I have a generic interface, say IGeneric. 我有一个通用接口,比如IGeneric。 For a given type, I want to find the generic arguments which a class imlements via IGeneric. 对于给定的类型,我想找到一个类通过IGeneric实现的泛型参数。

It is more clear in this example: 在这个例子中更清楚:

Class MyClass : IGeneric<Employee>, IGeneric<Company>, IDontWantThis<EvilType> { ... }

Type t = typeof(MyClass);
Type[] typeArgs = GetTypeArgsOfInterfacesOf(t);

// At this point, typeArgs must be equal to { typeof(Employee), typeof(Company) }

What is the implementation of GetTypeArgsOfInterfacesOf(Type t)? GetTypeArgsOfInterfacesOf(Type t)的实现是什么?

Note: It may be assumed that GetTypeArgsOfInterfacesOf method is written specifically for IGeneric. 注意:可以假设GetTypeArgsOfInterfacesOf方法是专门为IGeneric编写的。

Edit: Please note that I am specifically asking how to filter out IGeneric interface from all the interfaces that MyClass implements. 编辑:请注意我特别询问如何从MyClass实现的所有接口中过滤掉IGeneric接口。

Related: Finding out if a type implements a generic interface 相关: 确定类型是否实现了通用接口

To limit it to just a specific flavor of generic interface you need to get the generic type definition and compare to the "open" interface ( IGeneric<> - note no "T" specified): 要将它限制为通用接口的特定风格,您需要获取泛型类型定义并与“open”接口进行比较( IGeneric<> - 注意没有指定“T”):

List<Type> genTypes = new List<Type>();
foreach(Type intType in t.GetInterfaces()) {
    if(intType.IsGenericType && intType.GetGenericTypeDefinition()
        == typeof(IGeneric<>)) {
        genTypes.Add(intType.GetGenericArguments()[0]);
    }
}
// now look at genTypes

Or as LINQ query-syntax: 或者作为LINQ查询语法:

Type[] typeArgs = (
    from iType in typeof(MyClass).GetInterfaces()
    where iType.IsGenericType
      && iType.GetGenericTypeDefinition() == typeof(IGeneric<>)
    select iType.GetGenericArguments()[0]).ToArray();
typeof(MyClass)
    .GetInterfaces()
    .Where(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IGeneric<>))
    .SelectMany(i => i.GetGenericArguments())
    .ToArray();
  Type t = typeof(MyClass);
            List<Type> Gtypes = new List<Type>();
            foreach (Type it in t.GetInterfaces())
            {
                if ( it.IsGenericType && it.GetGenericTypeDefinition() == typeof(IGeneric<>))
                    Gtypes.AddRange(it.GetGenericArguments());
            }


 public class MyClass : IGeneric<Employee>, IGeneric<Company>, IDontWantThis<EvilType> { }

    public interface IGeneric<T>{}

    public interface IDontWantThis<T>{}

    public class Employee{ }

    public class Company{ }

    public class EvilType{ }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类型实现另一个接口的通用接口 - Generic interfaces which type implements another interface 确定泛型类是否实现类型 - Determine if Generic Class Implements Type 检查类型是否在不考虑泛型类型参数的情况下实现泛型接口 - Check if a type implements a generic interface without considering the generic type arguments 注册使用开放泛型类型将多个接口实现为单例的服务 - Registering service that implements several interfaces as singleton using open generic type 确定 class 是否实现通用接口,然后调用接口方法 - Determine if a class implements a generic interface and then call a the interfaces method .Net core, Service Collection - class 实现多个通用接口 - .Net core, Service Collection - class implements multiple generic interfaces 如何判断类是否实现了泛型类型的接口 - How to Tell if a Class implements an interface with a generic type 泛型类型参数是实现接口的类 - Generic type parameter is an class that implements an interface 具有通用类型的基类,该基类实现具有通用类型的接口 - Base class with a generic type which implements an interface with a generic type 使用SimpleInjector为实现泛型类型的类获取单例实例,而不实际返回单例 - Getting a singleton instance using SimpleInjector for a class which implements a generic type, not actually returning a singleton
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM