I need to invoke an overloaded method using reflection. My classes as below:
public static Transformer
{
//Overloaded method with generics parameter. First Transform Method
public static TranformerResult Transform<T>(object [] entities,
List<T> dataContract) where T:class
{
return transformerResult;
}
//Overloaded method without generics parameter. Second Transform Method
public static TranformerResult Transform(object entities,
Type dataContract)
{
return transformerResult;
}
}
public class TransformerResult
{
public List<T> GetTypes<T>() where T:class
{
}
}
I tried to invoke first Transform method with below syntax:
GetMethod(“Transform”,(BindingFlags.Static | BindingFlags.Public),
null, new Type[](){typeof(object[]),typeof(List<Type>}, null)
But I am getting second Transform method.
My intention is to invoke GetType on transformerResult . The transformerResult is an object which returns invocation of first transform method.
Can any one help me to write the C# code to achieve my intention?
Thanks, Mahir
The reason you're running into a problem is that the particular method you're looking for is generic. One of the types of the method is based off of the generic parameter of the method. This presents you with a bit of a catch 22. The generic parameter is tied to the method so you cannot properly construct the type array for the method until you have the method itself.
One solution for this specific scenario is the just grab the first generic method.
var method = typeof(Transformer).GetMethods().Where(x => x.IsGenericMethod).First();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With