在对象上调用通用扩展方法?

时间:2021-08-22 20:45:15

I have created a Generic Extension method for DataRow object. The method takes no argument. I want to Invoke the Generic method through Reflection using MethodInfo. I can do this for Normarl public methods, but somehow I cannot get the reference of the Generic Extension method.

我为DataRow对象创建了一个通用扩展方法。该方法不需要参数。我想通过Reflection使用MethodInfo调用Generic方法。我可以为Normarl公共方法做到这一点,但不知怎的,我无法获得Generic Extension方法的引用。

I've read this question on SO which somehwat relates to my query, but no luck that way.

我已经在SO上阅读了这个问题,但是我的问题与我的查询有关,但是没有运气。

1 个解决方案

#1


Keep in mind that extension methods are compiler tricks. If you look up the static method on the static class where the extension method is defined you can invoke it just fine.

请记住,扩展方法是编译器技巧。如果在定义扩展方法的静态类上查找静态方法,则可以很好地调用它。

Now, if all you have is an object and you are trying to find a particular extension method you could find the extension method in question by searching all your static classes in the app domain for methods that have the System.Runtime.CompilerServices.ExtensionAttribute and the particular method name and param sequence in question.

现在,如果您拥有的只是一个对象并且您正在尝试查找特定的扩展方法,则可以通过在app域中搜索具有System.Runtime.CompilerServices.ExtensionAttribute的所有静态类来查找有问题的扩展方法。有问题的特定方法名称和参数序列。

That approach will fail if two extension classes define an extension method with the same name and signature. It will also fail if the assembly is not loaded in the app domain.

如果两个扩展类定义具有相同名称和签名的扩展方法,则该方法将失败。如果程序集未在应用程序域中加载,它也将失败。

The simple approach is this (assuming you are looking for a generic method):

简单的方法就是这样(假设您正在寻找一种通用方法):

static class Extensions {
    public static T Echo<T>(this T obj) {
        return obj;
    }
}

class Program {

    static void Main(string[] args) {

        Console.WriteLine("hello".Echo());

        var mi = typeof(Extensions).GetMethod("Echo");
        var generic = mi.MakeGenericMethod(typeof(string));
        Console.WriteLine(generic.Invoke(null, new object[] { "hello" }));

        Console.ReadKey();
    }
}

#1


Keep in mind that extension methods are compiler tricks. If you look up the static method on the static class where the extension method is defined you can invoke it just fine.

请记住,扩展方法是编译器技巧。如果在定义扩展方法的静态类上查找静态方法,则可以很好地调用它。

Now, if all you have is an object and you are trying to find a particular extension method you could find the extension method in question by searching all your static classes in the app domain for methods that have the System.Runtime.CompilerServices.ExtensionAttribute and the particular method name and param sequence in question.

现在,如果您拥有的只是一个对象并且您正在尝试查找特定的扩展方法,则可以通过在app域中搜索具有System.Runtime.CompilerServices.ExtensionAttribute的所有静态类来查找有问题的扩展方法。有问题的特定方法名称和参数序列。

That approach will fail if two extension classes define an extension method with the same name and signature. It will also fail if the assembly is not loaded in the app domain.

如果两个扩展类定义具有相同名称和签名的扩展方法,则该方法将失败。如果程序集未在应用程序域中加载,它也将失败。

The simple approach is this (assuming you are looking for a generic method):

简单的方法就是这样(假设您正在寻找一种通用方法):

static class Extensions {
    public static T Echo<T>(this T obj) {
        return obj;
    }
}

class Program {

    static void Main(string[] args) {

        Console.WriteLine("hello".Echo());

        var mi = typeof(Extensions).GetMethod("Echo");
        var generic = mi.MakeGenericMethod(typeof(string));
        Console.WriteLine(generic.Invoke(null, new object[] { "hello" }));

        Console.ReadKey();
    }
}