<> An exception is thrown when the type initial value is set.

Source: Internet
Author: User
Type Initial Value Setting raises an exception

For two consecutive days, I was troubled by the "exception caused by type Initial Value Setting" error during the running, and the debugging did not know what went wrong. There are a lot of the same problems when surfing the Internet and Google, but the problem is still the same. Finally, I posted a post on csdn and asked, "a brave employee will be rewarded." Soon Some experts will reply and the problem will be solved. Haha. Write an essay here. If you encounter similar problems in the future, you can refer to it and make a memo on your own. Otherwise, you can't find it on your computer. My computer files are everywhere, sometimes I cannot even find pai_^ myself.
The problem is as follows:
The project adopts a three-tier architecture and a factory model, and draws on the petshop architecture. This project also uses distributed databases. Currently, there are only three databases, mainly to improve access performance.
It turned out to be an online Introduction to petshop to add references to various projects.
1. The web references BLL.
2. BLL references idal and model and uses dalfactory to create an instance.
3. The idal references the model.
4. The model is not referenced.
5. dalfactory references idal, reads the set assembly in Web. config, loads the class instance, and returns it to BLL for use.
6. sqlserverdal references model and idal, and is loaded by dalfactory to implement the methods in the interface.
The problem lies here. Top!
According to the petshop architecture, the corresponding class instances in the specific data access layer are created through reflection in the dalfactory Assembly. In this way, BLL knows how to call the implementation methods in the corresponding class instance when calling the interface.
Reflection dynamically loads the Assembly by using this method. load ("assembly "). createinstance ("namespace. class "), where the" assembly "reads the corresponding DLL under the bin folder of the web layer, that is, the reflected loaded Assembly dll must exist in the bin folder of the web layer, otherwise, an error occurs. Problem solved ~ Pai_^
The other two situations of this error are also seen on the Internet. Write them together.
1. Web. config configuration error.
In the dataaccess class in the dalfactory assembly
Private Static readonly string Path = configurationmanager. deleettings ["webdal"];
To obtain the Assembly name.
In web. config, my configuration is as follows:
<Deleetask>
<Add key = "webdal" value = "GBE. sqlserverdal"/>
</Appsettings>
2. incorrect assembly name and default namespace.
Right-click each assembly and choose Properties to check whether the Assembly name and default namespace are incorrect. This problem may also cause errors such as the question ~~

This problem occurs when we use reflection for the first time. We have learned a lot and have to deepen our understanding of reflection. ^_^

 

 

 

 

 

 

 

 

 

An exception occurs when you open the vs program compilation in the morning. The runtime error "exception caused by the Type Initial Value Setting item" suddenly occurs ". This error has not been found yesterday. It suddenly appeared today, making me confused. I searched the internet and found that many people encountered this problem. After some painstaking efforts to solve this problem, I found that many people did not realize the crux of the problem. So I wrote this essay to give you some inspiration and a memo.

Symptom description:
My program needs to connect to an MQ server (in fact, it is a desktop computer and shut down after work every day). If the server is not opened, the program will throw an exception. In the past, the server was always forgotten during compilation, and MQ access failures often occur. This time I thought it was also this exception. What I did not expect was that "the type Initial Value Setting item triggers an exception ".
Why is today's mistake different from the past? When I got off work yesterday, I reconstructed the single-piece mode of a class and used the static initialization method. The Code is as follows:
Static readonly workermanager instance = new workermanager ();
Static workermanager (){}
Private workermanager ()
{
Initialize ();
}
Public static workermanager instance
{
Get {return instance ;}
} The access to MQ statement is in the initialize method, and the problem should occur here.

Solution:
Of course, the problem of opening the MQ server is solved, but why does "type Initial Value Setting item cause an exception" occur?
If an exception occurs during initialization of static members of the original class, other static members of the category class or initialization of the class will throw this exception. See the following code:
Public class test
{
Public static Foo test1 = new Foo ();
Public static string Test2 = "Test2 ";
Public String test3 = "test3 ";
Public test (){}
}

Public class foo
{
Public Foo ()
{
Throw new exception ();
}
}

This exception is thrown when you access test. Test2 and new test. In my code, because initialize () encounters an exception and instance instantiation fails, this exception is thrown during instance access.

Summary:

Description of typeinitializationexeption in msnd: when the class Initial Value Setting item Cannot initialize the type, typeinitializationexception will be created and an exception reference thrown by the class Initial Value Setting item of this type will be passed to it.

We know that other static members in the class are initialized during type initialization or access type Static members, and static constructor is executed (if any ). In these processes, if the following code appears at any stage:


Class classhelper
{
Public static string field = Do ("Initial the static field ");
Public static string staticstring = "initaial static string ";
Public String nonstaticstring = "Initial non static string ";
Public classhelper ()
{
Nonstaticstring = "change non static string in instance constructor ";
Staticstring = "change static string in instance constructor ";
}
Public static string do (string field)
{
Console. writeline (field );
Throw new exception ();
Return field;
}
}

The typeinitializationexception may be caused by the following types:

1. A static member of the struct class, and initialization of other static members (or in the static constructor) produces an exception. For example, when classhelper. staticstring is accessed, A typeinitializationexception is thrown when classhelper. staticstring is called because an exception occurs during field initialization of the static member.

2. A static member of the struct class. initialization of the static member (or in the static constructor) produces an exception. For example, access classhelper. Field.

3. initialize the class, and an exception occurs when initializing a static member in the class (or in a static constructor. For example, classhelper helper = new classhelper ().

<> An exception is thrown when the type initial value is set.

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.