Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/drupal/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何创建接受多个值并序列化为WinForms Form.designer.cs的TypeConverter?_C#_.net_Winforms_Serialization_User Controls - Fatal编程技术网

C# 如何创建接受多个值并序列化为WinForms Form.designer.cs的TypeConverter?

C# 如何创建接受多个值并序列化为WinForms Form.designer.cs的TypeConverter?,c#,.net,winforms,serialization,user-controls,C#,.net,Winforms,Serialization,User Controls,我有一个抽象类,它有两个子类。值,以及StringValue或CustomValue。我想创建一个转换器来接受我需要的类,并很好地序列化它。有一个非常好的答案(但让我添加更多行以确保它接受空值) A型转换器 public class ValueConverter: SortedExpandableObjectConverter { private Value[] standardValues = new Value[3] { null, new StringValue(

我有一个抽象类,它有两个子类。值,以及StringValue或CustomValue。我想创建一个转换器来接受我需要的类,并很好地序列化它。

有一个非常好的答案(但让我添加更多行以确保它接受空值)

A型转换器

 public class ValueConverter: SortedExpandableObjectConverter
    {
        private Value[] standardValues = new Value[3] { null, new StringValue(), new CustomValue()};
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            if (sourceType == typeof(string)) return true;
            return base.CanConvertFrom(context, sourceType);
        }
        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            switch (value.ToString().Replace(" ", ""))
            {
                case "(None)":
                    return null;
                case nameof(StringValue):
                    return new StringValue();
                case nameof(CustomValue):
                    return new CustomValue();
            }
            return base.ConvertFrom(context, culture, value);
        }
        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
        {
            if (value == null) return "(None)"; // to accept null value.
            return base.ConvertTo(context, culture, value, destinationType);
        }
        public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context) => new StandardValuesCollection(standardValues);
        public override bool GetStandardValuesSupported(ITypeDescriptorContext context) => true;
        public override bool GetStandardValuesExclusive(ITypeDescriptorContext context) => true;
    }
A类

[TypeConverter(typeof(ValueConverter))]
public abstract class Value{}

public class StringValue : Value{ properties...}
public class CustomValue : Value{properties...}

   In UserControl declare property: 
   [DefaultValue(null)] // Is must for display null as grayed
   public Value MyValue{get;set;}