得到type,调用泛型函数例子

2021-06-08  本文已影响0人  价值投机168
namespace ConsoleApp3
{
public class AA
{
    public string name = "";
}

class Program
{
    public string aaaaa = "";
    public  void PrintTypeParameter<T>(string name1, int count, T aa)
    {
        Console.WriteLine(typeof(T) + " " + aa.GetType());
        Console.WriteLine(name1 + " " + count);
        Console.WriteLine((aa as AA)?.name);
        Console.WriteLine(this.aaaaa);
        Console.ReadKey();
    }

    static void Main()
    {
        Type type = typeof(Program);
        MethodInfo definition = type.GetMethod("PrintTypeParameter");
        MethodInfo constructed;
        Type a = typeof(AA);
        Program p = new Program();
        p.aaaaa = "hello ppppp";
        constructed = definition.MakeGenericMethod(a);
        constructed.Invoke(p, new object[] { "name1", 200, new AA() { name = "my name" } });
    }
}

}

上一篇 下一篇

猜你喜欢

热点阅读