少了个属性
public class TestControl : System.Windows.Forms.Control
{
private MyPoint testPoint;
[DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
public MyPoint TestPoint
{
get { return testPoint; }
set { testPoint = value; }
}
public TestControl()
{
this.Size = new Size(100,100);
this.Focus();
}
protected override OnPaint(PaintEventArgs pe)
{
base.OnPaint(pe);
pe.Graphics.FillRectangle(new SolidBrush(Color.Green),pe.ClipRectangle);
}
}
}it will generate the following code at InitializeComponent()
this.testControl1.TestPoint.I = 9;
this.testControl1.TestPoint.J = 9;
public class TestControl : System.Windows.Forms.Control
{
private MyPoint testPoint;
[DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
public MyPoint TestPoint
{
get { return testPoint; }
set { testPoint = value; }
}
public TestControl()
{
this.Size = new Size(100,100);
this.Focus();
}
protected override OnPaint(PaintEventArgs pe)
{
base.OnPaint(pe);
pe.Graphics.FillRectangle(new SolidBrush(Color.Green),pe.ClipRectangle);
}
}
}it will generate the following code at InitializeComponent()
this.testControl1.TestPoint.I = 9;
this.testControl1.TestPoint.J = 9;
IL_0000: ldc.i4.1
IL_0001: ret
两行. (可以在solution explorer中右键点击project name,选择property,在选build节点.可以设置编译选项)
this.testControl1.TestPoint.I = 9;
this.testControl1.TestPoint.J = 9;"
这种分开写的,我要的是this.testControl1.TestPoint = new Zhaixd.MyPoint(9.9);
而实际上,CLR类库的Point时不能够分开赋值的.并且使用时时不需要DesignerSerializationVisibility 这个属性的
你的MyPointConverter与系统的SizeConverter和PointConverter实际上还不完全一致。如果Visual Studio.NET要按照你所要求的那样
在改变属性窗口的I,J值的时候生成相应的ctor调用语句,那么Vs.net必须知道I/J和MyPoint(int i, int j)的参数之间的联系。Vs.net
不可能无端的猜测I/J恰好是ctor的参数,尤其是当MyPoint由其它的ctor或者Property的时候。所以,必须有你的程序显示的告诉VS.NET
这种内在的联系,而你上面的例子中未能建立这种联系,所以不能正常工作。
察看SizeConverter或者PointConverter的代码,不难发现在CanConvertTo中涉及了另外一个类型——InstanceDescriptor。InstanceDescriptor
用于描述一个对象,包括其构建所必需的信息。例如,其MemberInfo可以指向某个类的构件函数,Argments则包含ctor的参数,这样就可以
利用这些信息构件类对象,VS.NET也可以轻松的利用这些信息生成相应的ctor调用语句。因此,问题的关键在于你必须提供相应的方法将一个
MyPoint对象转换成一个InstanceDescriptor对象。这是通过ConvertTo实现的:、public override bool CanConvertTo(ITypeDescriptorContext context,Type destinationType)
{
if(destinationType == typeof(System.ComponentModel.Design.Serialization.InstanceDescriptor) )
return true;
return base.CanConvertTo(context,destinationType);、
}public override object ConvertTo(ITypeDescriptorContext context,CultureInfo culture,object value,Type destinationType)
{
if(destinationType == typeof(string) && value is MyPoint)
return ((MyPoint)value).I + ", " + ((MyPoint)value).J;
else if (destinationType == typeof(System.ComponentModel.Design.Serialization.InstanceDescriptor) && value is MyPoint)
{
Type t = typeof(MyPoint);
Type[] ts = new Type[2];
ts[0] = ts[1] = typeof(Int32);
ConstructorInfo ci = t.GetConstructor(ts); int[] vi = new int[2];
vi[0] = ((MyPoint)value).I;
vi[1] = ((MyPoint)value).J;
return new System.ComponentModel.Design.Serialization.InstanceDescriptor(ci, vi);
} return base.ConvertTo(context,culture,value,destinationType);
}
好开心啊,不但问题解决了,而且学会了IL代码指令,以后看源码方便了,哈哈
qqchen79你太强拉,非常佩服
冒昧问一句,你现在是在上学呢还是一经工作了??