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