Assembly assembly = Assembly.LoadFrom(@"..\..\..\Common\bin\Debug\Common.dll");Type t = assembly.GetType("Common.ForPrivate");MethodInfo method = t.GetMethod("Find", BindingFlags.NonPublic | BindingFlags.Instance);object obj = Activator.CreateInstance(t);obj result = method.Invoke(obj, new object[] { });
Assembly = assembly. loadfrom (@ "... \ .. \ common \ bin \ debug \ common. dll ");
Type T = assembly. GetType ("..");
In these two ways, the DLL of the referenced class is not obtained. Of course, it can also be an .exe file. Here, the number of .. \ is the number of parent directories pushed up in the current directory.
Naturally, if you add a reference to the target class in the current project, you can directly use type
T = typeof (forprivate );
Type T = assembly. GetType ("common. forprivate ");
This line, the string in the function, must be prefixed with a namespace.
Methodinfo method = T. getmethod ("find", bindingflags. nonpublic | bindingflags. instance );
The nonpublic and instance flags must be added at the same time. Otherwise, the corresponding private function will not be available.
Object OBJ = activator. createinstance (t );
You can also use this sentence instead:
Object OBJ = assembly. createinstance ("common. forprivate ");
Finally, Object Re = method. Invoke (OBJ, null)
If the called function has a return value, you must have an object type to accept it. Otherwise, an exception occurs.