Solution to an error in assembly. Load (PATH). createinstance (classname) in factory Mode

Source: Internet
Author: User
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



 



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.