110,533
社区成员
发帖
与我相关
我的任务
分享
[TypeConverter(typeof(ssConverter))]
public struct StructType
{
public StructType(int v1, int v2, int v3)
{
_p1 = v1;
_p2 = v2;
_p3 = v3;
}
public int P1
{
get { return _p1; }
set { _p1 = value; }
}int _p1;
public int P2
{
get { return _p2; }
set { _p2 = value; }
} int _p2;
public int P3
{
get { return _p3; }
set { _p3 = value; }
}int _p3;
}
class ssConverter : ExpandableObjectConverter
{
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
if (destinationType == typeof(System.ComponentModel.Design.Serialization.InstanceDescriptor) ||
destinationType == typeof(string))
return true;
return base.CanConvertTo(context, destinationType);
}
public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
{
if (destinationType == typeof(System.ComponentModel.Design.Serialization.InstanceDescriptor))
{
return new System.ComponentModel.Design.Serialization.InstanceDescriptor(typeof(StructType).GetConstructor(new Type[] { typeof(int), typeof(int), typeof(int) }),
new object[] { ((StructType)value).P1, ((StructType)value).P2, ((StructType)value).P3 });
}
if (destinationType == typeof(string))
{
string s = ((StructType)value).P1 + "," + ((StructType)value).P2 + "," + ((StructType)value).P3;
return s;
}
return base.ConvertTo(context, culture, value, destinationType);
}
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, System.Globalization.CultureInfo culture, object value)
{
if (value.GetType() == typeof(string))
{
string[] cs = ((string)value).Split(',');
StructType v = default(StructType);
int i;
if (int.TryParse(cs[0], out i))
v.P1 = i;
if (int.TryParse(cs[1], out i))
v.P2 = i;
if (int.TryParse(cs[2], out i))
v.P3 = i;
return v;
}
return base.ConvertFrom(context, culture, value);
}
}
public StructType V
{
get { return _v; }
set { _v = value; }
} StructType _v;
this.folderViewTree1.V = new XXXX.Windows.Forms.StructType(2, 3, 5);