简体   繁体   English

使用反射调用 generics 重载方法

[英]Invoke generics overloaded method using reflection

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:我尝试使用以下语法调用第一个 Transform 方法:

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.我的意图是在transformerResult 上调用GetType。 The transformerResult is an object which returns invocation of first transform method. TransformerResult 是一个 object,它返回第一个转换方法的调用。

Can any one help me to write the C# code to achieve my intention?任何人都可以帮助我编写 C# 代码来实现我的意图吗?

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.这为您提供了一些注意事项 22。泛型参数与方法相关联,因此在您拥有方法本身之前,您无法正确构造该方法的类型数组。

One solution for this specific scenario is the just grab the first generic method.这种特定场景的一个解决方案是只获取第一个通用方法。

var method = typeof(Transformer).GetMethods().Where(x => x.IsGenericMethod).First();

Unless this works: typeof(List<>) you're going to need to be a bit tricky with what you do, similar to what I had to do with this post: http://www.aaron-powell.com/posts/2010-04-08-reflection-and-generics.html除非这行得通: typeof(List<>)你需要对你所做的事情有点棘手,类似于我对这篇文章所做的事情: http://www.aaron-powell.com/posts /2010-04-08-reflection-and-generics.html

Essentially using LINQ to search the collection of all Transform methods, you can't get back just the one you want natively.本质上是使用 LINQ 来搜索所有Transform方法的集合,你不能只取回你本机想要的那个。

I think that the second type in the array of types in the call to GetMethod() is confusing things.我认为调用GetMethod()的类型数组中的第二种类型令人困惑。 If you get an instance of the Type class of the class containing these methods, that does not include the actual generic parameter used to create that instance of the class (which it looks like is Type , based on your second code sample).如果您获得包含这些方法的 class 的 class Type的实例,则不包括用于创建该 class 实例的实际通用参数(它看起来是基于您的第二个示例的Type )。 Instead, it just knows that it has one generic parameter.相反,它只知道它有一个通用参数。

I don't have a compiler in front of my currently, so unfortunately I can't try this out, but I would try to specify the equivalent of typeof(List<T>) for the second parameter (you might be able to get this by calling System.Collections.Generic.List.GetType() , but I'm not positive).我目前没有编译器,所以很遗憾我无法尝试这个,但我会尝试为第二个参数指定typeof(List<T>)的等效项(你可能会得到这是通过调用System.Collections.Generic.List.GetType()来实现的,但我不肯定)。

If that doesn't work, the only other option that I can think of is to call Type.GetMethods(BindingFlags.Static | BindingFlags.Public) , and search through the array yourself.如果这不起作用,我能想到的唯一其他选择是调用Type.GetMethods(BindingFlags.Static | BindingFlags.Public) ,然后自己搜索数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM