Generic constructors and reflection

前端 未结 4 949
说谎
说谎 2021-02-14 17:25

Is it possible to see which constructor was the generic one?

internal class Foo
{
  public Foo( T value ) {}
  public Foo( string value ) {}
}

var cons         


        
4条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-14 17:29

    You could check the Type.GetGenericArguments result type(s), and compare that to the constructor parameter type.

    Just call the one with a type that is not the same (type != typeof(T)).

提交回复
热议问题