zoukankan      html  css  js  c++  java
  • 反射泛型方法

    最近在拜读C# in depth这本书,在书中的反射泛型方法这一小节中遇到了一个小问题

    class Class1
            {
                 internal static void PrintType<T>()
                {
                    Console.WriteLine(typeof(T));
                }
            }
    
    
    static void main(string args[])
    {
                        Type typeClass1 = typeof(Class1);
                MethodInfo method = typeClass1.GetMethod("PrintType");
                MethodInfo contructMethod = method.MakeGenericMethod(typeof(string));
                contructMethod.Invoke(null, null);
                Console.ReadKey();
    }

    主函数运行后没有输出,并且出现一个Bug:
     MethodInfo contructMethod = method.MakeGenericMethod(typeof(string)); 未将对象引用到实例。

    经过排查发现是由于PrintType函数的访问权限的问题。

    在获取反射泛型方法时,该方法的访问权限必须为public,其他的权限不能让MakeGenericMethod()获取该方法。

  • 相关阅读:
    android 显示自定义视图对话框
    android为按钮事件进行监听过程
    实验三
    实验二
    实验一
    第五次作业
    第四次作业
    第三次作业
    第二次作业
    第一次作业
  • 原文地址:https://www.cnblogs.com/huangweikun/p/4766965.html
Copyright © 2011-2022 走看看