1. Problem Description★Code display
Below is a piece of code in the factory:
'*************************************** * ****** 'File Name: dataacess 'namespace: factory 'internal volume:' function: Create the interface required by the user 'file relationship:' OPERATOR: make the child busy 'small group:' generation Date: 17:37:52 'version No.: V2.0' modify log: 'copyright description: '*************************************** * ****** imports system. reflection 'references the reflection namespace imports system. configuration public class dataacess private shared readonly assemblyname as string = "Dal" 'data Assembly name dim strdb as string = system. configuration. configurationsettings. appsettings ("DB ") ''' <summary> ''' create user interface ''' </Summary> ''' <returns> return iuser </returns> ''' <remarks> </Remarks> Public Function createiuser () as idal. iuser dim classname as string = assemblyname + ". "+ strdb +" userdal "'defines the namespace. class <PRE name = "code" class = "VB"> <span style = "white-space: pre"> </span> return ctype (assembly. load (assemblyname ). createinstance (classname), idal. iuser) 'errors are easy to understand.
In this Code, the following statement is difficult to understand and prone to errors.
Return CType(Assembly.Load(assemblyName).CreateInstance(className), IDAL.IUser)
★Knowledge
Next, let's first understand assembly. Load (PATH). createinstance (classname)
In VB. NET, we need to use reflection. First, we need to understand the relationships between several classes in the following namespace:
System. Reflection namespace
(1) appdomain: application domain, which can be understood as a logical container of a group of assemblies
(2) Assembly: Assembly class
(3) module: module class
(4) type: Use reflection to obtain the core class of the type information.
An appdomain can contain N assemblies, an assembly can contain N modules, and a module can contain N types.
In the preceding statement, we use the Assembly class. In the program, what if we want to dynamically load an assembly? There are several ways to use static methods of Assembly: load, loadfrom, and loadwithpartialname. We mainly talk about the load method:
The usage of Assembly. Load ("") is described as follows;
It is not a namespace. Commonly used is the Assembly name, that is, the DLL name.
Reload list
Description
Assembly. Load (assemblyname) loads an Assembly Based on the assemblyname of the given assembly.
2. Problems and Solutions★Failed to Load file or assembly "Dal" or one of its Dependencies, the system cannot find the specified file
Reflection assembly. load ("assembly "). createinstance ("namespace. class "), regardless of the layer at which the (" assembly ") written in this Code reads the DLL in the bin folder of the web layer, that is to say, the Assembly DLL of the class you reflected must exist under the bin of the web layer.
Note that createinstance () must be the namespace. Class Name; otherwise, the created instance is empty.
Assembly. Load ("assembly name ")
There are three possibilities for such an error:
1). the DLL file name is inconsistent with the DLL file name during loading.
2). The. dll file does not exist at all, that is, the file is lost.
3) The DLL file is incorrectly loaded, that is, the DLL file exists, but the loading path is incorrect.
I have read my own questions, mainly in the third case above. The loading path is incorrect. How can this problem be solved?
Find the folder Dal/bin/debug, copy the files related to the Dal to the UI/bin/debug file, and run the file. Later work, it will be very troublesome to manually add each time in this case, so we need to change the program generation path:
★The instance where the object reference is not set to the object
In the createinstance (classname) statement above, classname is actually the full name of the type to be reflected (including the full path of the namespace). For example, the final result returned by classname is dal. sqluserdal.
It corresponds to the Dal layer, so the class name under the Dal layer must be consistent with it