使用反射调用带有签名的对象实例上的泛型方法:SomeObject.SomeGenericInstanceMethod(T argument)

发布于 2024-10-12 19:31:56 字数 481 浏览 3 评论 0原文

如何调用 SomeObject.SomeGenericInstanceMethod(T arg)

有几篇关于调用泛型方法的文章,但不太像这篇文章。问题在于方法参数参数被限制为泛型参数。

我知道如果签名是

SomeObject.SomeGenericInstanceMethod(string arg)

获取 MethodInfo

那么我可以使用typeof (SomeObject).GetMethod("SomeGenericInstanceMethod", new Type[] {typeof (string)}).MakeGenericMethod(typeof(GenericParameter))

那么,当常规参数是泛型类型时,如何获取 MethodInfo 呢?谢谢!

此外,通用参数可能有也可能没有类型约束。

How do I call SomeObject.SomeGenericInstanceMethod<T>(T arg) ?

There are a few posts about calling generic methods, but not quite like this one. The problem is that the method argument parameter is constrained to the generic parameter.

I know that if the signature were instead

SomeObject.SomeGenericInstanceMethod<T>(string arg)

then I could get the MethodInfo with

typeof (SomeObject).GetMethod("SomeGenericInstanceMethod", new Type[]{typeof (string)}).MakeGenericMethod(typeof(GenericParameter))

So, How do I go about getting the MethodInfo when the regular arguments are of a generic type? Thanks!

Also, there may or may not be type constrains on the generic parameter.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

唱一曲作罢 2024-10-19 19:31:56

你用完全相同的方式来做。

当您调用 MethodInfo.Invoke 时,您无论如何都会传递 object[] 中的所有参数,因此您不必在编译时知道类型。

样本:

using System;
using System.Reflection;

class Test
{
    public static void Foo<T>(T item)
    {
        Console.WriteLine("{0}: {1}", typeof(T), item);
    }

    static void CallByReflection(string name, Type typeArg,
                                 object value)
    {
        // Just for simplicity, assume it's public etc
        MethodInfo method = typeof(Test).GetMethod(name);
        MethodInfo generic = method.MakeGenericMethod(typeArg);
        generic.Invoke(null, new object[] { value });
    }

    static void Main()
    {
        CallByReflection("Foo", typeof(object), "actually a string");
        CallByReflection("Foo", typeof(string), "still a string");
        // This would throw an exception
        // CallByReflection("Foo", typeof(int), "oops");
    }
}

You do it exactly the same way.

When you call MethodInfo.Invoke, you pass all the arguments in an object[] anyway, so it's not like you have to know the types at compile time.

Sample:

using System;
using System.Reflection;

class Test
{
    public static void Foo<T>(T item)
    {
        Console.WriteLine("{0}: {1}", typeof(T), item);
    }

    static void CallByReflection(string name, Type typeArg,
                                 object value)
    {
        // Just for simplicity, assume it's public etc
        MethodInfo method = typeof(Test).GetMethod(name);
        MethodInfo generic = method.MakeGenericMethod(typeArg);
        generic.Invoke(null, new object[] { value });
    }

    static void Main()
    {
        CallByReflection("Foo", typeof(object), "actually a string");
        CallByReflection("Foo", typeof(string), "still a string");
        // This would throw an exception
        // CallByReflection("Foo", typeof(int), "oops");
    }
}
贩梦商人 2024-10-19 19:31:56

您以完全相同的方式执行此操作,但传递对象的实例:

typeof (SomeObject).GetMethod(
       "SomeGenericInstanceMethod", 
        yourObject.GetType())  
                 // Or typeof(TheClass), 
                 // or typeof(T) if you're in a generic method
   .MakeGenericMethod(typeof(GenericParameter))

MakeGenericMethod 方法只需要您指定泛型类型参数,而不是方法的参数。

当您稍后调用该方法时,您将传递参数。然而,此时,它们作为对象传递,所以这也没关系。

You do it exactly the same way, but pass an instance of your object:

typeof (SomeObject).GetMethod(
       "SomeGenericInstanceMethod", 
        yourObject.GetType())  
                 // Or typeof(TheClass), 
                 // or typeof(T) if you're in a generic method
   .MakeGenericMethod(typeof(GenericParameter))

The MakeGenericMethod method only requires you to specify the generic type parameters, not the method's arguments.

You'd pass the arguments in later, when you call the method. However, at this point, they're passing as object, so it again doesn't matter.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文