List<int> designer serialization

99封情书 提交于 2019-12-08 07:17:24

问题


Why MyIntList property below correctly interprets the DesignerSerializationVisibility.Content producing the following output in the designer:

 this.myControl1.MyIntList.Add(1);
 this.myControl1.MyIntList.Add(2);
 this.myControl1.MyIntList.Add(3);

while MyClassIntList one outputs the following?

this.myButton1.MyClassIntList = new MyClass(((System.Collections.Generic.List<int>)(resources.GetObject("myControl1.MyClassIntList"))));

Here is custom control class source code:

[Serializable]
public class MyControl : Control
{
    public MyControl()
        : base()
    {
    }

    private List<int> myIntList = new List<int>(new int[] { 1, 2, 3 });

    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    public List<int> MyIntList
    {
        get { return myIntList; }
        set { myIntList = value; }
    }

    internal bool ShouldSerializeMyIntList()
    {
        return true;
    }

    private MyClass myClassIntList = new MyClass(new List<int>(new[] { 1, 2, 3 }));

    public MyClass MyClassIntList
    {
        get { return myClassIntList; }
        set { myClassIntList = value; }
    }

    internal bool ShouldSerializeMyClassIntList()
    {
        return true;
    }

}

Here is the complete Form1.cs source code, in the case you want to test it:

public partial class MyForm : Form
{
    public MyForm()
    {
        InitializeComponent();
    }
}

[Serializable]
public class MyControl : Control
{
    public MyControl()
        : base()
    {
    }

    private List<int> myIntList = new List<int>(new int[] { 1, 2, 3 });

    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    public List<int> MyIntList
    {
        get { return myIntList; }
        set { myIntList = value; }
    }

    internal bool ShouldSerializeMyIntList()
    {
        return true;
    }

    private MyClass myClassIntList = new MyClass(new List<int>(new[] { 1, 2, 3 }));

    public MyClass MyClassIntList
    {
        get { return myClassIntList; }
        set { myClassIntList = value; }
    }

    internal bool ShouldSerializeMyClassIntList()
    {
        return true;
    }

}

[Serializable, TypeConverter(typeof(MyClassConverter))]
public class MyClass
{
    public MyClass(List<int> list)
    {
        myIntList = list;
    }

    private List<int> myIntList = new List<int>(new int[] { 1, 2, 3 });

    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    public List<int> MyIntList
    {
        get { return myIntList; }
        set { myIntList = value; }
    }
}

class MyClassConverter : ExpandableObjectConverter
{

    public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
    {
        if (destinationType == typeof(InstanceDescriptor))
            return true;
        return base.CanConvertTo(context, destinationType);
    }

    public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
    {
        if (value is MyClass)
        {

            if ((destinationType == typeof(InstanceDescriptor)))
            {
                MyClass myClass = (MyClass)value;

                object[] properties = new object[1];
                Type[] types = new Type[1];

                types[0] = typeof(List<int>);
                properties[0] = myClass.MyIntList;

                ConstructorInfo ci = typeof(MyClass).GetConstructor(types);
                return new InstanceDescriptor(ci, properties);
            }

        }

        return base.ConvertTo(context, culture, value, destinationType);
    }

    public override bool GetCreateInstanceSupported(ITypeDescriptorContext context)
    {
        return true;
    }

    public override object CreateInstance(ITypeDescriptorContext context, IDictionary propertyValues)
    {
        return new MyClass((List<int>)propertyValues["MyIntList"]);
    }

}

来源:https://stackoverflow.com/questions/6410216/listint-designer-serialization

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!