TypeInitilizationException的解决办法

    准备写一个静态类Config,这个类有静态成员ConnectionString,准备用来读取配置文件中的加密的数据库连接字符串。静态成员当然在静态构造函数中初始化,于是这样写:
    public class Config
    {
        // 数学试题库的数据库连接字符串
        private static string _mathDbConnectionString = null;
        public static string MathDbConnectionString
        {
            get
            {
                return _mathDbConnectionString;
            }
        }
        static Config()
        {
            // Db.Config是程序中使用的运行目录下的配置文件,Db.Config.Config是实际的配置文件
            string configFile = Application.StartupPath + @"/DB.Config";
            string configRealFile = Application.StartupPath + @"/DB.Config.Config";

            // 配置文件不存在,抛出“文件不存在”的异常
            if (!File.Exists(configFile) || !File.Exists(configRealFile))
            {
                thorw new System.IO.FileNotFoundException("配置文件不存在。");
            }

            // 读取Db.Config文件中的数据库连接字符串
            try
            {
                Configuration config = ConfigurationManager.OpenExeConfiguration

(Application.StartupPath + @"/DB.Config");
                _mathDbConnectionString = config.ConnectionStrings.ConnectionStrings

["MathConnStr"].ConnectionString;
            }
            catch (Exception ex)  // 如果有异常发生,说明配置文件格式错
            {
                throw ex;
            }
    }
    结果,在Db.Config文件能够正常读取的时候没有问题。当Db.Config文件不存在的时候,获取MathDbConnnectionString属性值时总是出现:TypeInitializationException异常,类型初始值设定项引发异常。
    很是郁闷,为什么获取的不是FileNotFoundException异常呢?这样的代码(在有问题时抛出异常)是没有问题的呀?
    最终在“IT设计者之家”上找到这样一篇文章:类型初始值设定项引发异常原因及解决方法(http://www.it55.com/html/xueyuan/chengxukaifa/_NETjiaocheng/20071123/261484.html),其中提到:原来类的静态成员在初始化时如果出现异常,访问类的其它静态成员或对该类进行初始化都会抛出这个异常。如果类中存在静态成员,应确保其初始化时不会抛出异常,否则会影响对该类的正常访问。  
    于是,将上面的静态构造函数改成如下:
    static Config()
        {
            // Db.Config是程序中使用的运行目录下的配置文件,Db.Config.Config是实际的配置文件
            string configFile = Application.StartupPath + @"/DB.Config";
            string configRealFile = Application.StartupPath + @"/DB.Config.Config";

            // 配置文件不存在,设置连接字符串和连接对象为空值
            if (!File.Exists(configFile) || !File.Exists(configRealFile))
            {
                _mathDbConnectionString = null;
                _mathDbConnection = null;
                return;
            }

            // 读取Db.Config文件中的数据库连接字符串
            try
            {
                Configuration config = ConfigurationManager.OpenExeConfiguration(Application.StartupPath + @"/DB.Config");
                _mathDbConnectionString = config.ConnectionStrings.ConnectionStrings["MathConnStr"].ConnectionString;
            }
            catch   // 如果有异常发生,说明配置文件格式错
            {
                _mathDbConnectionString = null;
                _mathDbConnection = null;
                return;
            }
    这样,如果获取的MathDbConnectionString值是null,就可以说明没有正确读取到数据库连接字符串了。

 

    2008年12月18日

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值