C# 反射调用类型中的方法,两种方式

public static class Demo
    {
        public static void MapGrpcService<TService>() where TService : class
        {
            Console.WriteLine(typeof(TService));
        }
        public static void Hello<T>(string name)
        {
            Console.WriteLine(typeof(T));
            Console.WriteLine(name);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
             Assembly assembly = Assembly.GetExecutingAssembly();
            Type type = assembly.GetType("every_FanShe.Demo");
            type.GetMethod("Hello").MakeGenericMethod(typeof(Demo)).Invoke(null, new[] { "wocao" });
            typeof(Demo).GetMethod("Hello").MakeGenericMethod(typeof(Demo)).Invoke(null,new[] { "wocao"});
            Console.Read();
        }
    }
posted @ 2020-01-08 15:18  ZaraNet  阅读(624)  评论(0编辑  收藏  举报