It's easy to get the GenericParameterPosition
for a parameter when its type is a generic type argument of the method:
public class MyClass {
public void Foo<T>(T a) { }
}
// ...
Console.WriteLine("pos: " + typeof(MyClass)
.GetMethod("Foo")
.GetParameters()
.Single()
.ParameterType
.GenericParameterPosition);
pos: 0
But how can we accomplish this for a parameter who's type is a generic type argument of the class?
public class MyClass<T> {
public void Foo(T a) { }
}
// ...
Console.WriteLine("pos: " + typeof(MyClass<int>)
.GetMethod("Foo")
.GetParameters()
.Single()
.ParameterType
.GenericParameterPosition);
InvalidOperationException: Method may only be called on a Type for which Type.IsGenericParameter is true.
Aucun commentaire:
Enregistrer un commentaire