Assembly.Load (Path). CreateInstance Reflection Error Resolution

Source: Internet
Author: User

The recent use of Factory mode to reflect the DAL layer there are some problems, so I would like to write their own thinking standard solutions and solutions for later use.

1. This is the project structure

2. This is the Dalfactory reflection code

 #regionCreate an object (without using a cache)/// <summary>        ///create an object (without using a cache)/// </summary>        /// <param name= "AssemblyPath" ></param>        /// <param name= "Classnamespace" ></param>        /// <returns></returns>        Private ObjectCreateobjectnocache (stringAssemblyPath,stringclassnamespace) {            Try{Assembly ASSM= Assembly.Load (assemblypath);//First step: Loading an assembly from the assembly name                ObjectObjType =ASSM. CreateInstance (Classnamespace);//Step Two: Create an instance of the class from the namespace + class name.                 returnObjType; }            Catch(Exception ex) {//Logoperate.writeerrorlog ("Create reflection exception (not using cache)", ex);                return NULL; }        }        #endregion

I split the launch into two steps to facilitate commissioning:

The first step: throughAssembly NameLoading assemblies (note that the assembly name, not the namespace)
Assembly ASSM = Assembly.Load (assemblypath);

If after debugging it is the error in this step:System.IO.FileNotFoundException: Failed to load file or assembly "Sqlserverdal" or one of its dependencies. The system cannot find the file specified.

Then there are two reasons:

One is that your assembly name is wrong and your assembly name can be viewed through the project-Properties-application.

The second is that the Web site or Windows application must reference the items you want to reflect.

For example, my Windows application here is SXPM must refer to Sqlserverdal.

Step two: Throughnamespace. Class nameCreates an instance of the class.

If the error occurs after debugging, such as when you invoke the instantiated class, the object is not referenced by the instance, then the reason is that your class name is wrong or the namespace is wrong .

Assembly.Load (path). CreateInstance Reflection Error Resolution

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.