[英]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.