C#,无法理解此错误?

C#,无法理解此错误?,c#,.net,C#,.net,我正在使用VS2008。我有一个项目,SystemSoftwareproject,连接数据库,我们正在使用L2E。我有一个RuntimeInfo类,其中包含一些共享信息。看起来是这样的: public class RuntimeInfo { public const int PWD_ExpireDays = 30; private static RuntimeInfo thisObj = new RuntimeInfo(); publi

我正在使用VS2008。我有一个项目,
SystemSoftware
project,连接数据库,我们正在使用
L2E
。我有一个
RuntimeInfo
类,其中包含一些共享信息。看起来是这样的:

public class RuntimeInfo
    {
        public const int PWD_ExpireDays = 30;

        private static RuntimeInfo thisObj = new RuntimeInfo();

        public static string AndeDBConnStr = ConfigurationManager.ConnectionStrings["AndeDBEntities"].ConnectionString;

        private RuntimeInfo() {

        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns>Return this singleton object</returns>
        public static RuntimeInfo getRuntimeInfo()
        {
            return thisObj;
        }
}
public class DbAccess : IDisposable
    {
        private String connStr = String.Empty;
        public DbAccess()
        {          
            connStr = RuntimeInfo.AndeDBConnStr;
        }
}
我的
SystemSoftware
工作正常,这意味着
RuntimeInfo
类没有问题。但是当我运行我的
和dataviewer
时,上面构造函数中的语句

connStr = RuntimeInfo.AndeDBConnStr;
,引发异常。例外情况复制到此处:

System.TypeInitializationException was unhandled
  Message="The type initializer for 'MyCompany.SystemSoftware.SystemInfo.RuntimeInfo' threw an exception."
  Source="AndeDataViewer"
  TypeName="MyCompany.SystemSoftware.SystemInfo.RuntimeInfo"
  StackTrace:
       at AndeDataViewer.DbAccess..ctor() in C:\workspace\SystemSoftware\Other\AndeDataViewer\AndeDataViewer\DbAccess.cs:line 17
       at AndeDataViewer.Window1.rbRawData_Checked(Object sender, RoutedEventArgs e) in C:\workspace\SystemSoftware\Other\AndeDataViewer\AndeDataViewer\Window1.xaml.cs:line 69
       at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
      ....
InnerException: System.NullReferenceException
       Message="Object reference not set to an instance of an object."
       Source="AndeDataViewer"
       StackTrace:
            at MyCompany.SystemSoftware.SystemInfo.RuntimeInfo..cctor() in C:\workspace\SystemSoftware\SystemSoftware\src\systeminfo\RuntimeInfo.cs:line 24
       InnerException: 
我无法理解这一点,因为我觉得这很好,但为什么会有例外?当一个类是链接类时,我们不能访问静态变量?我认为链接类应该与本地类相同。这里的“链接”是指当我添加文件时,我使用“添加为链接”

我怎样才能避免这个例外

EDIT:
我将系统软件的App.config作为链接添加到DataViewer。然后解决了这个问题,但我不喜欢它。如果我已经有App.config了呢?我也不想在那里手动复制connectionstring,因为“手动”也不好


对于那些有多个项目共享一个数据库的项目,当您遇到问题时,此链接可能会有所帮助:

如果您的application.config文件未定义连接字符串名称“AndeDBEntities”,您将看到此链接

线路

    public static string AndeDBConnStr = 
       ConfigurationManager.ConnectionStrings["AndeDBEntities"].ConnectionString;

正在访问由
ConnectionStrings[“AndeDBEntities”]
返回的对象的
ConnectionString
属性,该属性可能不存在。

如果应用程序.config文件未定义连接字符串名称“AndeDBEntities”,您将看到这一点

线路

    public static string AndeDBConnStr = 
       ConfigurationManager.ConnectionStrings["AndeDBEntities"].ConnectionString;

正在访问由
ConnectionString[“AndeDBEntities”]
返回的对象的
ConnectionString
属性,该属性可能不存在。

静态构造函数中存在异常。显式创建它并将所有静态初始值设定项移到那里。然后调试:)

静态构造函数中出现异常。显式创建它并将所有静态初始值设定项移到那里。然后调试:)

AndeDataViewer项目没有配置项。假设这是Winforms应用程序,则需要在该项目中定义app.config。

AndeDataViewer项目没有配置项。假设这是一个Winforms应用程序,您需要在该项目中定义一个app.config。

这是否意味着在我的AndeDataViewer中,我需要对SystemSoftware项目的app.config文件进行引用?项目中的引用在运行时实际上没有任何相关性。当项目中有App.config时,Visual Studio会将其复制到生成文件夹并重命名为AssemblyName.ext.config。因此,它不必存在于项目中,但它必须存在于您运行应用程序的任何位置。这是否意味着在我的AndeDataViewer中,我需要对SystemSoftware项目的App.config文件进行引用?项目中的引用在运行时并不真的具有任何相关性。当项目中有App.config时,Visual Studio会将其复制到生成文件夹并重命名为AssemblyName.ext.config。因此,它不必出现在项目中,但它必须出现在运行应用程序的任何位置。什么数据类型是
ConfigurationManager.connectionString
?我假设
Dictionary
但是为什么需要从它获取ConnectionString?对于依赖项,您可以将
andDataViewer
作为一个依赖项
SystemSoftware
,然后通过
SystemSoftware
命名空间访问其对象,尽管您还需要将
RuntimeInfo
公开才能执行此操作。什么数据类型是
ConfigurationManager.connectionString
?我假设
Dictionary
但是为什么需要从它获取ConnectionString?对于依赖项,您可以将
andDataViewer
作为一个依赖项
SystemSoftware
,然后通过
SystemSoftware
命名空间访问其对象,尽管您还需要将
RuntimeInfo
公开才能做到这一点。