Transferred from: http://blog.csdn.net/mm33211/article/details/8143890
C # Creates an object instance based on the full name of the object class
/// <summary> ///returns an object instance based on the specified class name/// </summary> /// <param name= "Objfullname" >the full name of the object (package name and class name), such as: Com.xxx.Test</param> Public ObjectCreateobjectinstance (stringobjfullname) { //Get current directory stringCurrentdir =assembly.getexecutingassembly (). Location; Currentdir= Currentdir.substring (0, Currentdir.lastindexof ('\\')); DirectoryInfo di=NewDirectoryInfo (Currentdir); //get all DLL files in the current directoryfileinfo[] files = di. GetFiles ("*.dll");//find only. dll files//traverse all files to find the implementation definition of the desired objectType type =Type.GetType (objfullname); if(Type = =NULL) { foreach(FileInfo fiinchfiles) {Type=Getobjecttype (FI. FullName, Objfullname); if(Type! =NULL) { Break; } } } if(Type = =NULL) { //throw new Exception ("Can not find class define of" + objfullname); return NULL; } //instantiating an object Objectobj=activator.createinstance (type); returnobj; } /// <summary> ///finds the specified object definition from the DLL file/// </summary> /// <param name= "Dllfile" >DLL file path</param> /// <param name= "Objfullname" >the full name of the object (package name and class name), such as: Com.xxx.Test</param> /// <returns>Returns the corresponding type if found, or null if not found</returns> PrivateType Getobjecttype (stringDllfile,stringobjfullname) {Type type=Assembly.loadfile (dllfile). GetType (Objfullname); if(Type! =NULL) {Console.WriteLine ("Find obj in dll["+ Dllfile +"]"); returntype; } return NULL; }
C # Creates an object instance based on the full name of the object class