Today, I saw a post on the forum, reflected a method, I thought it was very simple, huh, huh, the result, there is an OUT parameter in the method to launch; but still plagued for a while, the problem is solved, still shared it. . Build a class: using system;
Namespace WinFormTest {///
}
Public int getflag () {return 1;}
Public int result {result = 1; return 2;}
}} Use the following method to reflect: system.Type mytype = typeof (testReflection); // Reflection generated object instance object obj = mytype.invokememember (null, bindingflags.createInstance, null, null, null); system. INT32 i = new system.int32 (); object [] myargs = new object [] {i}; //messagebox.show (i.tostring ()); //Messagebox.show(((- )myargs[0] ) .Tostring ()); // Reflection Call Getflag method with OUT parameters mytype.invokemember ("getflag", bindingflags.invokeMethod, null, obj, myargs; //messagebox.show (i.toString ()) //Messagebox.show ((int) Myargs [0]). Tostring ()); //messagebox.show (Mytype.Invokemember ("getflag", bindingflags.invokeMethod, null, obj, myargs) .tostring () ); What is the most important thing is to handle that part of the parameter, myargs [0] is the parameter object returned by the reflection method, and converts it to the INT type for the data you want. It is not possible to directly read the previous I variables; this is because there is a value type and a reference type of packing operation, the value returned by the reflection should be saved in the Object object on the hosted stack. The last three messagebox.show methods return values, named 0, 1, 2, I still like to use the type.invokememember () method to reflect the type of member, the only insufficient is not to reflect the event member. Use other methods to reflect such methods such as MethodInfo, etc.