Reflection generic method, reflection generic
I recently read the book C # in depth, and encountered a small problem in the section of the reflection generic method in the book.
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 ();}
After the main function is run, there is no output and a Bug occurs:
MethodInfo contructMethod = method. MakeGenericMethod (typeof (string); the object is not referenced to the instance.
It is found that the access permission of the PrintType function is incorrect.
When obtaining a reflection generic method, the access permission of this method must be public. Other permissions cannot be obtained by MakeGenericMethod.