![](/img/trans.png)
[英]Visual Studio Extensibility - Custom Language Text Editor Settings
[英]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.