c# Reflection - Find the Generic Type of a Collection

前端 未结 3 1148
说谎
说谎 2021-02-18 14:43

I\'m reflecting a property \'Blah\' its Type is ICollection

    public ICollection Blah { get; set; }

    private void button1_Click(object sender         


        
3条回答
  •  悲哀的现实
    2021-02-18 15:01

    You'll want to look at GetGenericTypeDefinition for example:

       List strings=new List();
    
    
            Console.WriteLine(strings.GetType().GetGenericTypeDefinition());
            foreach (var t in strings.GetType().GetGenericArguments())
            {
                Console.WriteLine(t);
    
            }
    

    This will output:

    System.Collections.Generic.List`1[T]
    System.String

提交回复
热议问题