Exception in C #: "The type initializer to throw an exception (type initializer throws an exception)" Simple analysis and resolution

Source: Internet
Author: User
Tags cdata

For the exception in C #: "The type initializer to throw an exception (type initializer throws an exception)" Simple analysis, currently I analyze two cases, as follows:

Situation One:

Learn about Kylin. NET type initializer throws an exception article!!! Written in very detailed, you can have a look!

actually Kirin. NET This article mainly explains the analysis of the static members of the aspect, the general is: type initialization or access to the static members of the type, the class is initialized to other static members, if there is a static constructor, you can execute the static constructor. In the process of the exception caused by my direct reference to Kirin. NET Example:

classClasshelper { Public Static stringField = Do ("Initial the static field");  Public Static stringStaticstring ="initaial static string";  Public stringNonstaticstring ="Initial non static string";  PublicClasshelper () {nonstaticstring="Change non static string in instance constructor"; Staticstring="Change static string in instance constructor"; }         Public Static stringDo (stringfield)            {Console.WriteLine (field); Throw NewException (); returnfield; }    }
View Code

The typeinitializationexception scenario is that when a static member is instantiated, an exception is thrown whenever there is a static member instantiation, and the entire program runs with the title "the type initializer to Throw an exception (type initializer throws an exception) " exception, this is because static members of the nature of the decision, specific about the static, we can look online!" Here is not a detailed description of the situation of one by one examples, recommended this article has a detailed description! Here is a direct summary!

Workaround:

Since the above has been pointed out the nature of the exception, then we can step to trace the origin of the anomaly, by setting breakpoints, F11 to debug, if some can not see, then the specific operation example to see the situation two, I encountered the situation and solve.

Situation Two:

  specific content is not written , rather than static member instantiation directly caused by, we have to step-by-step analysis and resolution.

Running my project is generating an exception (the "Bingosoft.Data.DaoFactory" type initializer throws an exception) as follows:

Exception information thrown in the Web page:

The above is also our normal analysis of the exception thrown by the general steps, in the Web page we get the exception source, we have to analyze the situation is there any possibility???

Here my project is on the basis of the company development Framework Kissu.net, a lot of things are closed, we do not see the internal method implementation, so we pass vs to parse the corresponding DLL file, and throw the exception information, we can see is the Bingosoft.Data.dll file thrown in the exception, then we go to the item Find Bingosoft.Data.dll in the references, double-click to open:

The "abnormal indirect generating point" in the exception information graph thrown by the Web page is located in the location:

By combining the exception information in the Web page, we can infer the nature of the exception or the static member instantiation is caused, but it is worth noting that this is only indirect cause, the direct cause of the exception, we can see in the Web page exception information "InvalidOperationException" Has given us hints, so we start with the essence.

Workaround:

' key ' attribute must have a value of command in ' E:\VSProjects2010\Bingosoft.ProjectDemo\Bingosoft.ProjectDemo\ Bingosoft.projectdemo.webapp\app_config\sqlcommands\subway\subway.sqlserver.config '  Indicates that the Subway.SqlServer.config file must have a command value, let's look at this file! 
<? XML version= "1.0" encoding= "Utf-8" ?> < Configuration >    < system.web >    </ system.web > </ Configuration >
View Code

Look at a similar file content in the project:

<?XML version= "1.0" encoding= "Utf-8"?><commandsxmlns= "Http://schemas.bingosoft.net/netframework/sqlcommand">  <!--User Information List -  <CommandKey= "Demo.user.list">    <! [Cdata[Select Id id,name name,loginid loginid,sex Sex, (case if sex = ' M ' Then ' man ' when sex = ' F ' Then ' female ' els E ' End) Sexname, Birthday Birthday, OrgId OrgId, (select Name from sec_organization where id = su. OrgId) Deptname from Sec_user su where 1=1 {? and Name = #UserName #} {? and LoginId = #LoginId #}]]>  </Command>......</commands>
View Code

The original is in the project to add a functional module Subway, the results of the Subway.SqlServer.config file is not configured, then the project framework to run, all the referred files are parsed, and automatically parse the file, And so the exception happened. Originally I thought this module does not develop, to the entire project operation is not affected, but it turns out that the framework is to have all of the. config parsed, so unexpectedly, the exception occurred.

Now that we know, we can get started, add an empty command to him, and then look at the Subway.SqlServer.config file.

<?XML version= "1.0" encoding= "Utf-8"?><!--<configuration> <system.web> </system.web></configuration> -<commandsxmlns= "Http://schemas.bingosoft.net/netframework/sqlcommand">  <CommandKey= "Example">    <! [Cdata[......]]>  </Command></commands>
View Code

Re-rebuild under solution, run this project again, ok! This time it will be successful (not here, the project involves the company content, sorry!) But I really did run a success ~ ~)

Well, I'll write about it today.

Because of the limited technology, I hope you understand the inclusion, a lot of guidance ~ ~

If the written defect is not good, everyone commented that I will revise, thank you ~ ~

Exception in C #: "The type initializer to throw an exception (type initializer throws an exception)" Simple analysis and resolution

Related Article

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.