C # exception: Simple Analysis and Solution of "The type initializer to throw an exception (exception caused by type Initial Value Setting,
For a simple analysis of exceptions in C #: "The type initializer to throw an exception (exceptions are caused by type initial values)", I have analyzed The following two situations:
Scenario 1:
An exception article is triggered by referring to the type Initial Value Setting item of Qilin. NET !!! The description is very detailed. You can take a look!
In fact, Qilin. this article mainly analyzes static members. In general, other static members in the class are initialized during class initialization or access, if there is a static constructor, execute the static constructor together. In this process, I will directly learn from the example of Qilin. NET:
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 ;}}View Code
The TypeInitializationException is caused by an exception thrown when a static member is instantiated, the entire program is running with an exception such as The "The type initializer to throw an exception (exception caused by type Initial Value Setting)", which is determined by The nature of static members, for more information about static data, visit the internet! Here we will not describe in detail the case of various situations. We will describe this article in detail! Here is a summary!
Solution:
Now that we have already pointed out the essence of throwing an exception, we can track the exception generation point step by step and debug it by setting the breakpoint and F11. If some of them cannot be viewed, the specific operation example is Case 2: What I encountered and how to solve it.
Scenario 2:
The specific content is not written, rather than the direct result of static member instantiation. Let's analyze and solve it step by step.
Running my project generates an exception (an exception is thrown in the type of the "Bingosoft. Data. DaoFactory" Initial Value) as follows:
Exception information thrown on the webpage:
The above is also the general step for the exception thrown by our normal analysis. If we get an exception source on the webpage, can we analyze the situation first ???
Here, my project is in the development framework KissU. on the basis of Net, a lot of things are closed, so we can't see the internal method implementation, so we use VS to analyze the corresponding dll file and throw exception information, we can see that it is Bingosoft. data. if the exception is thrown in the dll file, we will find Bingosoft in the References of the project. data. dll, double-click to open:
We locate the "indirect exception generation point" in the exception information graph thrown out on the webpage:
Based on the exception information on the web page, we can infer that the essence of the exception is that static member instantiation is caused, but it is worth noting that this is only indirectly caused, we can see in the webpage exception information that "InvalidOperationException" has given us a prompt, so we start with the essence.
Solution:
'Key' attribute must has a value of command in 'e: \ VSProjects2010 \ Bingosoft. ProjectDemo. WebApp
\ App_Config \ SqlCommands \ Subway. SqlServer. config 'indicates that the Subway. SqlServer. config file must have a command value. Let's take a look at this file!
<? Xml version = "1.0" encoding = "UTF-8"?> <Configuration> <system. web> </system. web> </configuration>View Code
Look at a similar file in the project:
<? Xml version = "1.0" encoding = "UTF-8"?> <Commands xmlns = "http://schemas.bingosoft.net/netframework/sqlcommand"> <! -- User information list --> <command key = "demo. user. list"> <! [CDATA [select Id, Name, LoginId, Sex, (case when Sex = 'M' then' male 'when Sex = 'F' then' female 'else' end) SexName, Birthday, 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
Originally, a new function module Subway was added to the project. The result is Subway. sqlServer. the config file is not configured, so when the project framework is running, it resolves all the objects to be specified, and the file is parsed naturally, so an exception occurs. I originally thought that this module is not developed and has no impact on the operation of the entire project. However, it turns out that this framework has to put all of it. config is parsed, so unexpected exceptions occur.
Now that we know, we can start by adding an empty command to the Subway. SqlServer. config file.
<? Xml version = "1.0" encoding = "UTF-8"?> <! -- <Configuration> <system. web> </system. web> </configuration> --> <commands xmlns = "http://schemas.bingosoft.net/netframework/sqlcommand"> <command key = "example"> <! [CDATA [...]> </command> </commands>View Code
Rebuild Solution again, run this project again, OK! This is a success (this is not the case here. The project involves company content. Sorry! However, I am indeed running successfully ~~)
Now, let's write it here today ~~
Due to limited technology, I hope you can understand and provide more guidance ~~
If the writing is flawed, I will modify it in your comments. Thank you ~~