列表设计师系列化
为什么下面的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论