类型初始值设定项引发异常的解决办法

您所在的位置:网站首页 类型初始值设定项引发异常是什么意思 类型初始值设定项引发异常的解决办法

类型初始值设定项引发异常的解决办法

2024-07-09 21:36| 来源: 网络整理| 查看: 265

今天在调试代码的时候突然抛出了如下异常:“XORM.Database”的类型初始值设定项引发异常。

顿时感觉很突兀,平常的时候一点问题没有,为什么今天调试就出问题了呢?测试了一下,发现在数据处理层的一条实例化代码处出错:

//获取类型的映射信息 MappingInfo mapInfo = xmlMapping.GetDataMapInfo(type); Database db = new Database();//出错代码 db.CommandText = storageprocedure; //获取查询条件的映射信息

找了很久没有找到原因,后再网上搜索了下,是因为静态成员初始化异常引起的问题。我的Database类中只有一个静态成员。Database类如下:

using System; using System.Collections.Generic; using System.Text; using System.Data.SqlClient; using System.Data; using System.Collections; using System.Configuration; namespace XORM { internal class Database: IDisposable { private static string _connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["TestForCSSConn"].ConnectionString;//静态成员 private IDbConnection _connection = null; private string _commandText = null; private ArrayList _parameters = new ArrayList(); private bool _disposed; public IDataReader GetDataReader() { using (IDbCommand cmd = getCommand()) { return cmd.ExecuteReader(); } } private ArrayList _inoutParameters = new ArrayList(); private IDbCommand getCommand() { IDbCommand cmd = connection.CreateCommand(); cmd.CommandText = _commandText; cmd.CommandType = CommandType.StoredProcedure; foreach (SqlParameter parm in _parameters) { cmd.Parameters.Add(parm); } //既能输入也能返回的参数 foreach (SqlParameter parm in _inoutParameters) { parm.Direction = ParameterDirection.Output; cmd.Parameters.Add(parm); } return cmd; } public IDbCommand GetCommand() { return getCommand(); } public string CommandText { set { _commandText = value; } get { return _commandText; } } private IDbConnection connection { get { if (_connection == null) { _connection = new SqlConnection(_connectionString); _connection.Open(); } return _connection; } } public void AddParameters(string name, object obj) { if (name.StartsWith("@") == false) { name = "@" + name; } SqlParameter p = new SqlParameter(name, obj); _parameters.Add(p); } public void AddInOutParameters(string name, object obj) { if (name.StartsWith("@") == false) { name = "@" + name; } SqlParameter p = new SqlParameter(name, obj); _inoutParameters.Add(p); } public ArrayList Parameters { get { return _parameters; } } public void Dispose() { if (_disposed == true) { return; } // Dispose and close the connection Close(); // This object will be cleaned up by the Dispose method. // Therefore, you should call GC.SupressFinalize to // take this object off the finalization queue // and prevent finalization code for this object // from executing a second time. GC.SuppressFinalize(this); _disposed = true; } public void Close() { if (_disposed == true) { return; } if (_connection != null) { _connection.Close(); _connection.Dispose(); } } } }

仔细对比了一下,发现确实是这条语句出了问题,在我的web.config配置文件中数据库连接字符串的没有名为TestForCSSConn的,这是因为今天修改了web.config文件,新的数据库连接字段如下:

 

 

将此处的name="TestForCSS"修改为name="TestForCSSConn"就可以了。

通过在网上搜索归纳了一下可能的原因:

1. 访问类的某一静态成员,而其他静态成员的初始化(或静态构造函数)中产生异常。例如访问ClassHelper.StaticString,由于静态成员Field的初始化产生异常,因此调用ClassHelper.StaticString会抛出TypeInitializationException。

2. 访问类的某一静态成员,该静态成员的初始化(或静态构造函数)中产生异常。

3. 对该类进行初始化,而类中的某个静态成员初始化(或静态构造函数)中产生异常。

解决办法:

1、检查类中的静态字段或属性,确保其能够正常的完成初始化

2、在类的构造函数中,确保其访问的属性或字段都已经完成初始化

3、如果是WinForm中,将访问的窗体控件的语句写在初始化方法之后

同时,导致错误发生还可能是导入的引用sqlite.dll上,以下是这种情况的解决办法(引用自博友雪庭):

sqlite.dll分32位和 64位,以前用在32位下,开发换到win 7/x64下。在项目属性中,修改目平台,从Any CPU改为x86,重新运行正 常,sqlite.dll是32位的,但目标 平台是x64的,有关sqlite的静态变量初始化异常,引起sqlite类初始化错误,引发 TypeInitializationException异常。

 



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3