How to get the type of T from a member of a generic class or method?

前端 未结 16 1927
梦毁少年i
梦毁少年i 2020-11-22 02:37

Let say I have a generic member in a class or method, so:

public class Foo
{
    public List Bar { get; set; }

    public void Baz()
    {         


        
16条回答
  •  清酒与你
    2020-11-22 03:07

    Using 3dGrabber's solution:

    public static T GetEnumeratedType(this IEnumerable _)
    {
        return default(T);
    }
    
    //and now 
    
    var list = new Dictionary();
    var stronglyTypedVar = list.GetEnumeratedType();
    

提交回复
热议问题