3.4.4 反射和泛型

        static void Main(string[] args)
        {
            Type type = typeof(Program);
            MethodInfo methodInfo = type.GetMethod("A");
            MethodInfo m = methodInfo.MakeGenericMethod(typeof(string));
            var a = m.Invoke(new Program(), new object[] { "222" });

            Console.ReadKey();
        }
        public static string A<T>(T s)
        {
            return s.ToSafeString();
        }

实际上,当 SomeType 只描述返回类型参数的操作时,协变就是安全的;而当 SomeType
只描述接受类型参数的操作时,逆变就是安全的

猜你喜欢

转载自www.cnblogs.com/kikyoqiang/p/9998261.html