Solved the problem of dynamic loading

zhaozj2021-02-16  94

Sure enough, after I checked some information online, I finally loaded the DLL and called the method inside. In fact, it is very simple after understanding. The specific implementation code is as follows:

Assembly ass;

Ass ("" // bin // kernel.dll ");" // bin // kernel.dll ");

TYPE mytype = ass.gettype ("kernel.dbcontrol");

MethodInfo mi = mytype.getMethod ("viewalldata");

Object [] arg = new object [1];

Arg [0] = Server.mappath ("");

Object obj = activator.createInstance (MyTYPE, ARG);

Test.dataSource = (Dataset) mi.invoke (Obj, null);

Test.DATABIND ();

The first first put the DLL. Then get a specific class name. Then get a method in this class. The most important sentence is: Object obj = activator.createInstance (MyType, arg); this effect is to instantiate this class. Note that instantiation can bring parameters (which looks like the constructor is like. After instantiating the class is the call method, as as the instantiation class, the passage of the parameters. Of course, it is best forced to convert a type.

转载请注明原文地址:https://www.9cbs.com/read-14549.html

New Post(0)