C# DefaultValueAttriubte如何进行编码?

C# DefaultValueAttriubte如何进行编码?,c#,reflection,properties,C#,Reflection,Properties,我用DefaultValueAttribute装饰了一个属性 代码如下所示: [DefaultValue(typeof(Encoding), "utf-8")] public Encoding Encoding { get; set; } 有一种重置方法可以恢复所有属性的所有默认值: public void Reset() { foreach (var property in TypeDescriptor.GetProperties(typeof(ILoggedChannelValue

我用
DefaultValueAttribute
装饰了一个属性

代码如下所示:

[DefaultValue(typeof(Encoding), "utf-8")]
public Encoding Encoding { get; set; }
有一种重置方法可以恢复所有属性的所有默认值:

public void Reset()
{
    foreach (var property in TypeDescriptor.GetProperties(typeof(ILoggedChannelValueFileExportInfo)).Cast<PropertyDescriptor>())
    {
        property.ResetValue(this);
    }
}
创建实例并调用
Reset
后,编码仍然为null,并且没有从
EncodingTypeConverter
调用任何方法。我该怎么办`

没有涉及UI-没有属性网格。唯一应该重置值的是TypeDescriptor

编辑: 几乎不可能编写自己的
TypeConverter
-调用,因为
DefaultValueAttribute
不存储指定的字符串值。疼。这是我的实现,它不起作用。也许有人可以用它来创建解决方案:

public virtual void ResetValues()
{
    foreach (var property in TypeDescriptor.GetProperties(typeof(TAttributedType)).Cast<PropertyDescriptor>())
    {
        if (property.CanResetValue(this))
        {
            property.ResetValue(this);
            continue;
        }

        var defaultValueAttribute = (DefaultValueAttribute)property.Attributes[typeof(DefaultValueAttribute)];
        var typeConverterAttribute = (TypeConverterAttribute)property.Attributes[typeof(TypeConverterAttribute)];
        if (defaultValueAttribute == null || !(defaultValueAttribute.Value is string) ||
            typeConverterAttribute == null || string.IsNullOrWhiteSpace(typeConverterAttribute.ConverterTypeName))
        {
            continue;
        }

        var typeConverterType = Type.GetType(typeConverterAttribute.ConverterTypeName);
        if (typeConverterType == null)
        {
            continue;
        }

        var typeConverter = (TypeConverter)Activator.CreateInstance(typeConverterType);
        if (typeConverter.CanConvertFrom(typeof(string)))
        {
            var propertyValue = typeConverter.ConvertFrom(defaultValueAttribute.Value);
            if (propertyValue != null)
            {
                property.SetValue(this, propertyValue);
            }
        }
    }
}
公共虚拟空间重置值()
{
foreach(TypeDescriptor.GetProperties(typeof(tatAttributedType)).Cast()中的var属性)
{
if(property.CanResetValue(this))
{
属性。重置值(此);
持续
}
var defaultValueAttribute=(defaultValueAttribute)property.Attributes[typeof(defaultValueAttribute)];
var typeConverterAttribute=(typeConverterAttribute)属性.Attributes[typeof(typeConverterAttribute)];
if(defaultValueAttribute==null | |!(defaultValueAttribute.Value为字符串)||
typeConverterAttribute==null | | string.IsNullOrWhiteSpace(typeConverterAttribute.ConverterTypeName))
{
持续
}
var typeConverterType=Type.GetType(typeConverterAttribute.ConverterTypeName);
if(typeConverterType==null)
{
持续
}
var typeConverter=(typeConverter)Activator.CreateInstance(typeConverterType);
if(typeConverter.CanConvertFrom(typeof(string)))
{
var propertyValue=typeConverter.ConvertFrom(defaultValueAttribute.Value);
if(propertyValue!=null)
{
SetValue(这是propertyValue);
}
}
}
}

添加
TypeConverterAttribute
并定义
默认值(typeof(Encoding),“UTF-8”)
将不起作用。
DefaultValue
的构造函数尝试从
TypeDescriptor
中查找不使用
TypeConverterAttribute
TypeConverter

下面是
ResetValues
-方法的实现,该方法使用
DefaultValueAttribute
中的字符串值和
TypeConverterAttribute
加载默认值

public virtual void ResetValues()
{
    foreach (var property in TypeDescriptor.GetProperties(typeof(TAttributedType)).Cast<PropertyDescriptor>())
    {
        var defaultValueAttribute = (DefaultValueAttribute)property.Attributes[typeof(DefaultValueAttribute)];
        var typeConverterAttribute = (TypeConverterAttribute)property.Attributes[typeof(TypeConverterAttribute)];
        if (defaultValueAttribute != null && defaultValueAttribute.Value is string &&
            typeConverterAttribute != null && !string.IsNullOrWhiteSpace(typeConverterAttribute.ConverterTypeName))
        {
            var typeConverterType = Type.GetType(typeConverterAttribute.ConverterTypeName);
            if (typeConverterType != null)
            {
                var typeConverter = (TypeConverter)Activator.CreateInstance(typeConverterType);
                if (typeConverter.CanConvertFrom(typeof(string)))
                {
                    var propertyValue = typeConverter.ConvertFrom(defaultValueAttribute.Value);
                    if (propertyValue != null)
                    {
                        property.SetValue(this, propertyValue);
                        continue;
                    }
                }
            }
        }

        if (property.CanResetValue(this))
        {
            property.ResetValue(this);
        }
    }
}
该物业为:

[TypeConverter(typeof(EncodingTypeConverter))]
[DefaultValue("UTF-8")]
public Encoding Encoding { get; set; }

请注意,
ResetValues
-方法首先测试
TypeConverter
和指定的
DefaultValue
。这是必需的,因为如果将字符串指定为默认值,则
PropertyDescriptor.CanResetValue()
将返回
true
。这不能转换为编码。

编码类没有类型转换器,您必须编写一个,并使用[TypeConverter]属性告诉设计器。
public class EncodingTypeConverter : TypeConverter
{
    public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
    {
        return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType);
    }

    public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
    {
        return value is string ? Encoding.GetEncoding((string)value) : base.ConvertFrom(context, culture, value);
    }
}
[TypeConverter(typeof(EncodingTypeConverter))]
[DefaultValue("UTF-8")]
public Encoding Encoding { get; set; }