简体   繁体   English

当参数值作为接口传递时,如何解析通用参数的Class类型?

[英]How can I resolve the Class type for generic parameter, when the parameter value is being passed as an interface?

Consider a method with the following signature: 考虑具有以下签名的方法:

void foo(List<T> myList) ...

Let's assume, by using reflection, you need to construct such a function and obtain the PropertyInfo details of the T type parameter. 假设通过反射,您需要构造这样的函数并获取T类型参数的PropertyInfo详细信息。 Calling typeof(T).GetProperties(...) should do the trick, so we can add the following line to our method, to obtain these details. 调用typeof(T).GetProperties(...)应该可以解决问题,因此我们可以typeof(T).GetProperties(...)添加到我们的方法中,以获取这些详细信息。

void foo(List<T> myList)
{
    PropertyInfo[] props = 
        typeof(T).GetProperties(BindingFlags.Public 
            | BindingFlags.Instance 
            | BindingFlags.FlattenHierarchy);

    ...
}

This provides the information we need... except when T is an interface parameter, what I'm finding is that props only contains the interface properties and not the properties associated with class that is within the list, which inherits from the interface. 这提供了我们需要的信息...除了当T是接口参数时,我发现的是props仅包含接口属性,而不包含与列表中的类相关的属性,该类是从接口继承的。

To be clear, my interface AND my class definitions are public, as are the properties within my classes. 明确地说,我的接口和我的类定义是公共的,我的类中的属性也是公共的。

How can I get the properties associated with the actual type that inherits an interface rather than strictly the interface properties? 如何获得与继承接口的实际类型关联的属性,而不是严格地继承接口属性?

If you want the actual type, you probably need to get it for each item: 如果您想要实际的类型,则可能需要为每个项目获取它:

foreach (T t in myList)
{
    Type itemType = t.GetType();

    itemType.GetProperties(...)
    // etc.
}

You can also add specific code for different types with: 您还可以使用以下方法为不同类型添加特定的代码:

if(itemType == typeof(MyConcreteType))
{
    // do specific stuff for that type
}

If you want to get a flat list of all object properties contained inside the List<T> , here's a LINQ solution: 如果要获取List<T>包含的所有对象属性的平面列表,请使用LINQ解决方案:

IEnumerable<PropertyInfo> properties =
                          myList.SelectMany(x => x.GetType()
                                                  .GetProperties(BindingFlags.Public | 
                                                                 BindingFlags.Instance));

If you need to access the declaring type, you can always look PropertyInfo.DeclaryingType 如果需要访问声明类型,则始终可以查看PropertyInfo.DeclaryingType

If you don't want a flat list, Select can do: 如果您不想使用固定列表,则“ Select可以执行以下操作:

IEnumerable<PropertyInfo[]> properties =
                            myList.Select(x => x.GetType()
                                                .GetProperties(BindingFlags.Public | 
                                                               BindingFlags.Instance));

暂无
暂无

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

相关问题 我可以将基本 class 类型作为通用参数传递给接口吗 - Can I pass a base class type as a generic parameter to an interface 我可以捕获由c#中的通用参数传递的类型的异常吗? - Can I catch an Exception of a type passed in by generic parameter in c#? 当T是具有未知参数的泛型类时,如何访问类型T的对象上的属性? - How can I access property on object of type T, when T is a generic class with unknown parameter? C#泛型如何获取没有对象类型作为参数的泛型方法的参数类型? - C# Generics How Can I get the type passed as an argument for a generic method with no object type as a parameter? 如何使用受约束的泛型类型参数将 class 实例化为派生接口 - How to instantiate a class as the interface that it derives from with constrained generic type parameter 泛型类型参数是实现接口的类 - Generic type parameter is an class that implements an interface 为什么我不能将&#39;as&#39;用于限制为接口的泛型类型参数? - Why can't I use 'as' with generic type parameter that is constrained to be an interface? 如何定义实现接口并约束类型参数的通用 class? - How do I define a generic class that implements an interface and constrains the type parameter? 可以将当前类的类型用作泛型类型参数的值吗? - Can the type of the current class be used as the value of a generic type parameter? 将通用接口参数类型转换为通用类类型 - Casting a Generic Interface Parameter Type to Generic Class Type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM