繁体   English   中英

如何使我的自定义类型出现在Visual Studio设置编辑器中?

[英]How to make my custom type to appear in Visual Studio Settings Editor?

假设我要使用一个自定义类型,以允许用户在app.config配置Person详细信息:

public class Person
{
  public string FirstName { get; set; }
  public string LastName { get; set; }
}

当我在项目的Visual Studio设置编辑器中单击“ Browse... ”时(在下拉“ Type ”的组合框之后),如何确保“ Person ”出现在类型列表中?

[ 1: ]
有关Select a Type Dialog Box MSDN文章中,我发现了这一点:

可用的引用程序集显示在树控件中。 打开引用的程序集以显示该程序集的名称空间。

因此,您的解决方案之一就是创建自己的程序集,将其引用到您的解决方案,然后可以在“ Type Dialog Box找到它。


在搜索时,我发现“ Type Dialog Box中的System.Drawing.Point可用( 具有此结构 )而System.Drawing.Rectangle不可用( 具有此结构 ),它们都是具有相同attributes struct ,因此我发现显示了一个在该Dialog Box Type内容并不取决于构造struct和使用某些attributes


[ 2: ]
换句话说,您可以使用具有fName; lName类的值的string类型设置fName; lName fName; lName并在类中添加构造函数或方法,以将您的类属性设置为所需的属性,如下所示:

public Person(string fullName, char seperator = ';')
{
    if (fullName.IndexOf(seperator) > 0)
    {
        firstname = fullName.Substring(0, fullName.IndexOf(seperator) - 1);
        lastname = fullName.Substring(fullName.IndexOf(seperator) + 1);
    }
}

[ 3: ]
这不是一个好方法,但是会引起注意,您可以通过在Settings.Designer.cs如下编辑来手动编辑设置的类型:

[global::System.Configuration.UserScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Configuration.DefaultSettingValueAttribute("fName; lName")]
public Person Test
{
    get {
        return ((Person)(this["Test"]));
    }
    set {
        this["Test"] = value;
    }
}

但这将迫使您对班级进行一些更改,并且每当保存然后Settings ,都应一次又一次地进行更改。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM