简体   繁体   English

反射:通用列表的非通用子类中包含的项类型

[英]Reflection: Type of item contained in non-generic subclass of generic list

public class MyList : List<MyClass>

How can I get the type MyClass through reflection if I have an object that contains an instance of MyList ? 如果我有一个包含MyList实例的object ,如何通过反射获取MyClass类型? The list can be empty, so I can't do something like myList[0].GetType() . 列表可以为空,所以我不能做像myList[0].GetType()这样的事情。

ps I can't just stop using MyList and directly use the generic List instead (the situation is a bit more complicated, and there are reasons for "hiding" the generic argument), so I can't pick up MyClass through GetGenericArguments() . ps我不能只停止使用MyList并直接使用通用List(情况有点复杂,并且有“隐藏”泛型参数的原因),所以我无法通过GetGenericArguments()获取MyClass

var elementType = (
    from iface in myList.GetType().GetInterfaces()
    where iface.IsGenericType
    where iface.GetGenericTypeDefinition() == typeof(IList<>)
    select iface.GetGenericArguments()[0])
        .Single();

I use IList<T> instead of list. 我使用IList<T>而不是list。 This is more generic. 这更通用。 However, there is also the change of a type implementing multiple ILis<T> versions (such as IList<string> and IList<int> ). 但是,还有一个实现多个ILis<T>版本的类型的更改(例如IList<string>IList<int> )。

You can get the base type, which will be List<MyClass> ; 你可以得到基类型,它将是List<MyClass> ; from it you can get the generic type argument with GetGenericArguments . 从中你可以使用GetGenericArguments获取泛型类型参数。

MyList list = new MyList();
Type baseTypeGenericArgument = list.GetType().BaseType.GetGenericArguments()[0];
string argumentTypeName = baseTypeGenericArgument.GetType().FullName;

Is your class implements generic interfaces? 你的类是否实现了通用接口? you can use the following code: 您可以使用以下代码:

Type argument = GetGenericArgument(typeof(MyList), typeof(IList<>));
//...
static Type GetGenericArgument(Type type, Type genericTypeDefinition) {
    Type[] interfaces = type.GetInterfaces();
    for(int i = 0; i < interfaces.Length; i++) {
        if(!interfaces[i].IsGenericType) continue;
        if(interfaces[i].GetGenericTypeDefinition() == genericTypeDefinition)
            return interfaces[i].GetGenericArguments()[0];
    }
    return null;
}

Without interfaces you can try the following: 没有接口,您可以尝试以下方法:

class A { }
class B { }
class G<T> { }
class G1<T> : G<A> { }
class G2 : G1<B> { }
//...
Type argument1 = GetGenericArgument(typeof(G2)); // B
Type argument2 = GetGenericArgument(typeof(G2),1 ); // A
//...
static Type GetGenericArgument(Type type, int level = 0) {
    do {
        if(type.IsGenericType && 0 == level--)
            return type.GetGenericArguments()[0];
        type = type.BaseType;
    }
    while(type != null);
    return null;
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM