简体   繁体   中英

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

Suppose I have a custom type I want to use to allow users to configure details for a Person in app.config :

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

How can I make sure " Person " appears in the list of types when I click on " Browse... " in the Visual Studio Settings Editor for the project (after dropping down the combo box for " Type ")?

[ 1: ]
At MSDN article about Select a Type Dialog Box I found this:

The available referenced assemblies display in a tree control. Open a referenced assembly to display namespaces for the assembly.

So, One of your solution is to make your own assembly, referencing it to your solution and you can find it in your Type Dialog Box .


As my searches I found that the System.Drawing.Point is available ( With this structure ) and the System.Drawing.Rectangle is not available ( With this structure ) in Type Dialog Box that both are struct with same attributes so I found showing a Type in that Dialog Box is not depended on making a struct and using some attributes .


[ 2: ]
In another way you can have a string type setting with a value like fName; lName fName; lName and add a constructor or a method to your class that sets your class properties to that you want like below:

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: ]
This is not a good way but can be noticed, You can edit the type of your setting manually by editing it in Settings.Designer.cs like below:

[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;
    }
}

But this will force you to make some changes on your class, and whenever you save then Settings you should do that changes again and again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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