简体   繁体   English

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

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

How to call some method via Reflection without any parameters and any return values? 如何通过反射调用一些没有任何参数和任何返回值的方法?

Here is MSDN sample 这是MSDN 示例

// 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? 那么typeof(???)内应该是什么?

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

Thank you!!! 谢谢!!!

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

If you were invoking that through C#, you would need to supply a type, for example: 如果要通过C#调用它,则需要提供一个类型,例如:

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);
    }
}

暂无
暂无

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

相关问题 如何实现一个可以接收任何参数并返回任何值的回调? - How to implement a callback that could receive any parameters and return any values? 如何通过反射在对象上调用方法 - How to call method on object via reflection 如何使用Reflection获取方法中的参数值? - How to get the values of the parameters in a method with Reflection? 如何通过反射从基于城堡的DynamicProxy中获取EventHandler(并调用订户(如果有)) - How to get an EventHandler from a Castle-based DynamicProxy (and call subscribers if any) via Reflection 我正在尝试为参数接受一些Ref参数的任何方法调用InvokeMethod,它给出了异常 - I am trying to call InvokeMethod for any method which arguments accept some Ref parameters it is giving exceptions 如何在没有任何参数的情况下创建和调用方法? - How can i make and call a method without any paramaters? 使用反射调用静态方法时得到错误的返回值? - Getting wrong return values when using reflection to call a static method? 如何通过反射执行带有可选参数的私有静态方法? - How to execute a private static method with optional parameters via reflection? 如何通过反射调用带参数的泛型方法? - How do you call a generic method with out parameters by reflection? 如何用反射方法调用这个function,参数是delegate和generics - How to call this function with reflection method,Parameters are delegates and generics
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM