Settings.Designer文件和静态性 [英] Settings.Designer file and Staticness

查看:196
本文介绍了Settings.Designer文件和静态性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个DAL类库,它作为DLL包含在我的程序中.下一行来自DAL,用于初始化连接.

I have a DAL class library that is included in my program as a DLL. The below line is from the DAL to initialize the connection.

DataSet ds = new DataSet("table");
        SqlConnection cnn = new SqlConnection(Settings.CMOSQLConn);

运行此命令时,出现以下错误:

When I run this I get the below error:

An unhandled exception of type 'System.StackOverflowException' occurred in CMO.DAL.dll

下面是Settings.Designer.cs文件中的文件,它是显示获取呼叫错误的地方:

The below is in the Settings.Designer.cs file and it is where it shows the error on the get call:

        [global::System.Configuration.ApplicationScopedSettingAttribute()]
    [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
    [global::System.Configuration.SpecialSettingAttribute(global::System.Configuration.SpecialSetting.ConnectionString)]
    [global::System.Configuration.DefaultSettingValueAttribute("Data Source=WWCSTAGE;Initial Catalog=CMO;Persist Security Info=True;User ID=CMOWe" +
        "bService;Password=ecivreSbeWOMC")]
    public static string CMOSQLConn {
        get {
            return (CMOSQLConn);
        }
    }

任何人对要寻找的东西有任何想法吗?是因为连接字符串存储在dll中而不是我的主应用程序中?我真的很坚持,将不胜感激!

Anyone have any ideas of what to look for? Is it because the connection string is stored in the dll instead of my Main App? I am really stuck on this and will greatly appreciate any help!

我尝试了以下 Greg的 建议:

I tried Greg's suggestion below:

        public static string CMOSQLConn {
        get {
            return (Settings.CMOSQLConn);
        }
    }

我仍然遇到相同的错误...还有其他想法吗?到目前为止,谢谢!

And I still get the same error... Any more thoughts? Thanks so far!

所以我遵循了下面重新生成设置文件的建议,现在我的设置文件看起来像这样->

So I followed the suggestion of regenerating the settings file below and now my setting file looks like this -->

public string CMOSQLConn {
        get {
            return ((string)(this["CMOSQLConn"]));
        }
    }

不幸的是,无论我在哪里发表此声明,现在都无法编译->

Unfortunately this won't compile now as wherever I have this statement -->

            SqlConnection cnn = new SqlConnection(Settings.CMOSQLConn);

我现在收到此错误->

I now get this error -->

Error   1   An object reference is required for the non-static field, method, or property 'CMO.DAL.Properties.Settings.CMOSQLConn.get'  B:\MyDocs\tmpPATRIOT\Projects\VS2008\DisConnectDAL\CMO.DAL\SupportWorker.cs 13  51  CMO.DAL

这是我应该期待的吗?

Is this what I should expect?

谢谢!

推荐答案

这是经典的c#属性错误.仔细检查您要归还的财产-您要归还财产本身!名称解析将优先使用本地名称而不是外部名称.之所以会出现堆栈溢出,是因为当CMOSQLConn.get调用CMOSQLConn.get时,您遇到了无限递归.

This is a classic c# properties mistake. Double check what you're returning in your property-- you're returning the property itself! Name resolution will prefer the local name over an external name. You're getting a stack overflow because you hit infinite recursion when CMOSQLConn.get calls CMOSQLConn.get.

考虑返回Settings.CMOSQLConn.额外的规范应清楚地表明您的连接字符串的正确位置.

Consider returning Settings.CMOSQLConn. The extra specification should clearly indicate the correct location of your connection string.

哇!我没有注意到您从设置"设计器文件中粘贴了该文件.无限递归显然正在发生,但恐怕您将不得不进行更多调查以找出这种情况下发生这种情况的原因.

Whoops! I didn't notice that you pasted that from your Settings designer file. The infinite recursion is clearly happening, but I'm afraid you'll have to do some more investigation to track down why it's happening in this case.

看来您的设计器文件生成不正确(!!!).在VS2008上,我的设置设计器获取器看起来像:

It appears that your designer file was generated incorrectly (!!!). On VS2008, my settings designer getters look something like:

public bool Foo{
    get {
        return ((bool)(this["Foo"]));
    }
    // ...
}

您可能需要执行类似的操作. IE浏览器:

You may need to do something similar. IE:

public string CMOSQLConn
    get {
        return ((string)(this["CMOSQLConn"]));
    }
    // ...
}

这篇关于Settings.Designer文件和静态性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆