Is there any way to tell via reflection that a generic list of Type A is related to a generic list of Type 开发者_如何学PythonB? For example, I have a List<string>
and a List<int>
. How can I tell via reflection that both these types are 'instances' of List<T>
. I think I'm having a problem because List<T>
isn't a real type. You can't do typeof(List<T>)
for example. List<T>
is a compiler trick I guess. So is there anyway to determine if two different types come from List<T>
?
Sure you can... List<>
is actually what's called an "unbound generic type" meaning it has not been parameterized with a type. When the type argument is specified it's called a "bound generic type". A type which involves "raw" type parameters, like List<T>
is an "open generic type", and one that involves only actual types, like List<int>
is a "closed generic type". The only situation in which an unbound generic type may be used in C# is in the typeof operator. To access the unbound type, or a closed type you would do:
Type listOfT = typeof(List<>); // unbound type
Type listOfString = typeof(List<string>); // closed bound type
Type listOfInt32 = typeof(List<int>); // closed bound type
Assert.IsTrue(listOfString.IsGenericType);
Assert.AreEqual(typeof(string), listOfString.GetGenericTypeParameters()[0]);
Assert.AreEqual(typeof(List<>), listOfString.GetGenericTypeDefinition());
Type setOfString = typeof(HashSet<string>);
Assert.AreNotEqual(typeof(List<>), setOfString.GetGenericTypeDefinition());
Actually List<T>
is a real type in many ways (you can use typeof(List<>)
, for example), and List<T>
is not merely a compiler trick, but a runtime trick. But you can indeed check the open generic type, via something like:
static Type GetRawType(Type type)
{
return type.IsGenericType ? type.GetGenericTypeDefinition() : type;
}
static void Main()
{
List<int> list1 = new List<int>();
List<string> list2 = new List<string>();
Type type1 = GetRawType(list1.GetType()),
type2 = GetRawType(list2.GetType());
Console.WriteLine(type1 == type2); // true
}
Try:
typeof(List<string>).GetGenericTypeDefinition() == typeof(List<int>).GetGenericTypeDefinition()
(see http://msdn.microsoft.com/en-us/library/system.type.getgenerictypedefinition.aspx)
Call GetGenericTypeDefinition:
List<string> l1 = new List<string>();
List<int> l2 = new List<int>();
Type t1 = l1.GetType().GetGenericTypeDefinition();
Type t2 = l2.GetType().GetGenericTypeDefinition();
Console.Write(t1 == t2);//output: true;
精彩评论