.NET设置文件可用类型

时间:2010-07-29 17:32:13

标签: .net settings type-systems settings.settings

修改.NET设置文件时,我可以选择设置类型。但是,即使在“浏览”窗口中,也不会出现我的项目可访问的所有类型。

是什么决定了一种类型是否可以用于设置文件设置?

我有一个我希望能够保存的类型,我想知道我需要更改它以在设置文件中使用它。

(VS 2008 - .Net 3.5)

2 个答案:

答案 0 :(得分:0)

您需要做的是“稍微”破解.settings和.Designer.cs文件,如文档here所述。

如果您在项目中创建自定义类型,例如:

namespace MyApp
{
    public struct MyType
    {
        public string StringValue;
    }
}

要使其在“设置”编辑器中显示为选项,您需要将使用该类型的第一个设置值添加到文件中,如下所示:

SettingsFile.settings:

<Setting Name="SettingNameGoesHere" Type="MyApp.MyType" Scope="User">
  <Value Profile="(Default)" />
</Setting>

SettingsFile.Designer.cs

[global::System.Configuration.UserScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
public global::MyApp.MyType SettingNameGoesHere {
    get {
        return ((global::MyApp.MyType)(this["SettingNameGoesHere"]));
    }
    set {
        this["SettingNameGoesHere"] = value;
    }
}

答案 1 :(得分:0)

“浏览”窗口中显示哪些类型 - 我认为答案是可序列化的类型。下面是一个适合我的例子(VS 2012,.Net 4.0)。

具有公共可序列化成员的类:

class Person {
    public string Name;
    public int Age;
}

在设计师的设定值中写什么:

<Person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <Name>John</Name>
    <Age>42</Age>
</Person>