列表设计师系列化

发布于 2024-11-16 09:19:52 字数 4005 浏览 4 评论 0原文

为什么下面的 MyIntList 属性可以正确解释 DesignerSerializationVisibility.Content 在设计器中生成以下输出:

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

MyClassIntList 则输出以下内容?

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

这是自定义控件类源代码:

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

}

这是完整的 Form1.cs 源代码,如果您想测试它:

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"]);
    }

}

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"]);
    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文