Generic Type Members

You can obtain member metadata for both unbound and closed generic types:

 
using System;
using System.Reflection;
using System.Collections.Generic;
class Program
{
    static void Main()
    {
        PropertyInfo unbound = typeof(IEnumerator<>).GetProperty("Current"); 
        PropertyInfo closed = typeof(IEnumerator<int>).GetProperty("Current");

        Console.WriteLine(unbound); // T Current
        Console.WriteLine(closed);  // Int32 Current
        Console.WriteLine(unbound.PropertyType.IsGenericParameter);  // True
        Console.WriteLine(closed.PropertyType.IsGenericParameter);  // False

    }
}
  

The output:


T Current
Int32 Current
True
False

The MemberInfo objects returned from unbound and closed generic types are always distinct:

 
using System;
using System.Reflection;
using System.Collections.Generic;
class Program
{
    static void Main()
    {
        PropertyInfo unbound = typeof(List<>).GetProperty("Count");
        PropertyInfo closed = typeof(List<int>).GetProperty("Count");

        Console.WriteLine(unbound); // Int32 Count
        Console.WriteLine(closed);  // Int32 Count

        Console.WriteLine(unbound == closed); // False

        Console.WriteLine(unbound.DeclaringType.IsGenericTypeDefinition); // True
        Console.WriteLine(closed.DeclaringType.IsGenericTypeDefinition); // False
    }
}

The output:


Int32 Count
Int32 Count
False
True
False
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.