Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在没有任何参数和返回值的情况下通过反射调用某个方法?_C#_.net_Reflection - Fatal编程技术网

C# 如何在没有任何参数和返回值的情况下通过反射调用某个方法?

C# 如何在没有任何参数和返回值的情况下通过反射调用某个方法?,c#,.net,reflection,C#,.net,Reflection,如何在没有任何参数和返回值的情况下通过反射调用某个方法 这是MSDN 谢谢你 在能够调用泛型方法之前,您需要指定其泛型参数。因此,传递要用作泛型参数的类型: public class Example { public static void Generic<T>() { Console.WriteLine("The type of T is: {0}", typeof(T)); } } class Program { static v

如何在没有任何参数和返回值的情况下通过反射调用某个方法

这是MSDN


谢谢你

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

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

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

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

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

Example.Generic<int>();
要获得完整的工作说明,请执行以下操作:

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);
    }
}
类示例
{
公共静态void Generic()
{
System.Console.WriteLine(“\r\n它是:{0}”,“完成”);
}
静态void Main()
{
var mi=typeof(示例).GetMethod(“通用”);
MakeGenericMethod(typeof(int)).Invoke(null,null);
}
}

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

Example.Generic<int>();
要获得完整的工作说明,请执行以下操作:

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);
    }
}
类示例
{
公共静态void Generic()
{
System.Console.WriteLine(“\r\n它是:{0}”,“完成”);
}
静态void Main()
{
var mi=typeof(示例).GetMethod(“通用”);
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);
    }
}