C # Creates an object instance based on the full name of the object class

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.