在设置自定义类型 [英] custom types in settings
问题描述
我怎么能在我的设置自己的类型。
How can I have my own types in Settings.
我成功让他们在设置表,但问题是,我不能设置默认值。 而问题是,我无法看到的app.config设置。
I succeed to have them in settings sheet, but the issue is that I can't set default values. And the problem is that I can't see the setting in app.config.
推荐答案
如果我跨preT你的问题吧,你有一个自定义类型,姑且称之为 CustomSetting
,以及你有哪些在该类型的你的 Settings.settings
文件中的设置,并指定使用该设置的默认值的app.config
或Visual Studio的设置界面。
If I interpret your question right, you have a custom type, let's call it CustomSetting
, and you which to have a setting in your Settings.settings
file of that type, and specify a default value for that setting using app.config
or Visual Studio's settings UI.
如果这是你想要做什么,你需要提供一个<一个href="http://msdn.microsoft.com/en-us/library/system.componentmodel.typeconverter.aspx"><$c$c>TypeConverter$c$c>为你的类型,可以从一个字符串转换,像这样的:
If that is what you want to do, you need to provide a TypeConverter
for your type that can convert from a string, like this:
[TypeConverter(typeof(CustomSettingConverter))]
public class CustomSetting
{
public string Foo { get; set; }
public string Bar { get; set; }
public override string ToString()
{
return string.Format("{0};{1}", Foo, Bar);
}
}
public class CustomSettingConverter : TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
if( sourceType == typeof(string) )
return true;
else
return base.CanConvertFrom(context, sourceType);
}
public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
{
string stringValue = value as string;
if( stringValue != null )
{
// Obviously, using more robust parsing in production code is recommended.
string[] parts = stringValue.Split(';');
if( parts.Length == 2 )
return new CustomSetting() { Foo = parts[0], Bar = parts[1] };
else
throw new FormatException("Invalid format");
}
else
return base.ConvertFrom(context, culture, value);
}
}
的一些背景信息
类型转换器的背后是大量的在.NET Framework中的字符串转换魔力。这不是设置只是有用的,它也是如何在Windows窗体和组件设计转换值从属性网格到他们的目标类型,以及如何XAML转换属性值。许多框架的种类有自定义类型转换器类,包括所有的基本类型,而且类型,如 System.Drawing.Size
或 System.Windows.Thickness
和很多很多人。
Some background information
TypeConverter is behind a lot of the string conversion magic in the .Net framework. It's not just useful for settings, it's also how the Windows Forms and Component designers convert values from the property grid to their target types, and how XAML converts attribute values. Many of the framework's types have custom TypeConverter classes, including all the primitive types, but also types like System.Drawing.Size
or System.Windows.Thickness
and many, many others.
使用类型转换器从自己的code是非常容易的,所有你需要做的是这样的:
Using a TypeConverter from your own code is very easy, all you need to do is this:
TypeConverter converter = TypeDescriptor.GetConverter(typeof(TargetType));
if( converter != null && converter.CanConvertFrom(typeof(SourceType)) )
targetValue = (TargetType)converter.ConvertFrom(sourceValue);
支持哪些源类型各不相同,但字符串
是最常见的一种。这是一个更强大的(不幸的是鲜为人知的)从字符串转换价值比的方式无处不在,但弹性较差转换
类(只支持基本类型)。
Which source types are supported varies, but string
is the most common one. It's a much more powerful (and unfortunately lesser known) way of converting values from strings than the ubiquitous but less flexible Convert
class (which only supports primitive types).
这篇关于在设置自定义类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!