如何通过反射调用一些没有任何参数和返回值的方法?

发布于 2024-12-20 03:48:11 字数 533 浏览 2 评论 0原文

如何通过反射调用一些没有任何参数和返回值的方法?

这是MSDN 示例< /a>

// Define a class with a generic method.
public class Example
{
    public static void Generic<T>()
    {
        Console.WriteLine("\r\nHere it is: {0}", "DONE");
    }
}

那么 typeof(???) 中应该包含什么?

MethodInfo miConstructed = mi.MakeGenericMethod(typeof(???));

谢谢你!!!

How to call some method via Reflection without any parameters and any return values?

Here is MSDN sample

// Define a class with a generic method.
public class Example
{
    public static void Generic<T>()
    {
        Console.WriteLine("\r\nHere it is: {0}", "DONE");
    }
}

What should be within typeof(???) then?

MethodInfo miConstructed = mi.MakeGenericMethod(typeof(???));

Thank you!!!

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

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

发布评论

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

评论(2

瀞厅☆埖开 2024-12-27 03:48:11

如果您通过 C# 调用它,则需要提供一个类型,例如:

Example.Generic<int>();

该要求不会改变;简而言之,该行将变为:

mi.MakeGenericMethod(typeof(int)).Invoke(null, null);

对于完整的、有效的说明:

class Example
{
    public static void Generic<T>()
    {
        System.Console.WriteLine("\r\nHere it is: {0}", "DONE");
    }
    static void Main()
    {
        var mi = typeof (Example).GetMethod("Generic");
        mi.MakeGenericMethod(typeof(int)).Invoke(null, null);
    }
}

If you were invoking that through C#, you would need to supply a type, for example:

Example.Generic<int>();

that requirement does not change; simply, that line would become:

mi.MakeGenericMethod(typeof(int)).Invoke(null, null);

For a complete, working illustration:

class Example
{
    public static void Generic<T>()
    {
        System.Console.WriteLine("\r\nHere it is: {0}", "DONE");
    }
    static void Main()
    {
        var mi = typeof (Example).GetMethod("Generic");
        mi.MakeGenericMethod(typeof(int)).Invoke(null, null);
    }
}
原野 2024-12-27 03:48:11

在能够调用泛型方法之前,您需要指定其泛型参数。因此,您传递要用作通用参数的类型:

public class Example
{
    public static void Generic<T>()
    {
        Console.WriteLine("The type of T is: {0}", typeof(T));
    }
}

class Program
{
    static void Main()
    {
        var mi = typeof(Example).GetMethod("Generic");
        MethodInfo miConstructed = mi.MakeGenericMethod(typeof(string));
        miConstructed.Invoke(null, null);
    }
}

它应该打印:

The type of T is: System.String

Before being able to invoke a generic method you need to specify its generic argument(s). So you pass the type you want to be used as generic argument:

public class Example
{
    public static void Generic<T>()
    {
        Console.WriteLine("The type of T is: {0}", typeof(T));
    }
}

class Program
{
    static void Main()
    {
        var mi = typeof(Example).GetMethod("Generic");
        MethodInfo miConstructed = mi.MakeGenericMethod(typeof(string));
        miConstructed.Invoke(null, null);
    }
}

which should print:

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