1. Design Interface Class:
public interface IBase{ void Somefun();}
2. Design extension plug-ins:
class PluginCls:IBase{ public void Somefun() { Console.WriteLine("hello ,i am derived class"); }}
3. Main Program Design:
Class program {static void main (string [] ARGs) {Assembly ASM = assembly. loadFile (@".... \ plugincls. DLL "); Type [] clstypes = ASM. gettypes (); // you can access all types defined in the Assembly. // type [] clstypes = ASM. getexportedtypes (); // This method can only access the public-defined type foreach (Type E in clstypes) {If (E. isclass & typeof (IBASE ). isassignablefrom (E) // compatible with {ibase mth = (IBASE) activator. createinstance (E); // class instance to construct MTH. somefun (); // call the implementation method of the plug-in class} console. readkey ();}}
Program output:
In this way, the main application can load plug-ins designed by other people, without worrying about the new types defined by the plug-in, as long as the plug-in is designed according to the given interface principles,
The main program can be seamlessly loaded.
Reference: CLR via C #, Chapter 2
Jeffrey's implementation is too embarrassing. The book is a classic book, and his book is recommended for the split of walls!