110,534
社区成员
发帖
与我相关
我的任务
分享
[PersistenceMode(PersistenceMode.InnerDefaultProperty)]
public MyStruct MyPro
<MyPro I="1" J="2" />
[DefaultProperty("Text")]
[ToolboxData("<{0}:HasCustomClassProperty runat=server></{0}:HasCustomClassProperty>")]
public class HasCustomClassProperty : WebControl
{
[TypeConverter(typeof(MyStructConverter))]
[Browsable(true)]
public MyStruct MyPro
{
get { return ViewState["MyPro"] == null ? default(MyStruct) : (MyStruct) ViewState["MyPro"]; }
set { ViewState["MyPro"] = value; }
}
protected override void RenderContents(HtmlTextWriter output)
{
output.Write(String.Format("i:{0}",MyPro.I));
output.Write(String.Format("j:{0}", MyPro.J));
}
}
public class MyStructConverter : ExpandableObjectConverter
{
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 is String)
{
String[] values = value.ToString().Split(',');
MyStruct result = new MyStruct(Byte.Parse(values[0]), Byte.Parse(values[1]));
return result;
}
else
{
return base.ConvertFrom(context, culture, value);
}
}
public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
{
if (destinationType == typeof(String))
{
MyStruct myStruct = (MyStruct) value;
return String.Format("{0},{1}", myStruct.I, myStruct.J);
}
else
{
return base.ConvertTo(context, culture, value, destinationType);
}
}
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
if (destinationType == typeof(String))
{
return true;
}
else
{
return base.CanConvertTo(context, destinationType);
}
}
}
[Serializable]
public struct MyStruct
{
private byte i;
private byte j;
public MyStruct(byte a, byte b)
{
i = a;
j = b;
}
public byte J
{
get { return j; }
set { j = value; }
}
public byte I
{
get { return i; }
set { i = value; }
}
}
[DefaultProperty("Text")]
[ToolboxData("<{0}:HasCustomClassProperty runat=server></{0}:HasCustomClassProperty>")]
public class HasCustomClassProperty : WebControl
{
[TypeConverter(typeof(MyStructConverter))]
[Browsable(true)]
public MyStruct MyPro
{
get { return ViewState["MyPro"] == null ? default(MyStruct) : (MyStruct) ViewState["MyPro"]; }
set { ViewState["MyPro"] = value; }
}
protected override void RenderContents(HtmlTextWriter output)
{
output.Write(String.Format("i:{0}",MyPro.i));
output.Write(String.Format("j:{0}", MyPro.j));
}
}
public class MyStructConverter : TypeConverter
{
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 is String)
{
String[] values = value.ToString().Split(',');
MyStruct result = new MyStruct(Byte.Parse(values[0]), Byte.Parse(values[1]));
return result;
}
else
{
return base.ConvertFrom(context, culture, value);
}
}
public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
{
if (destinationType == typeof(String))
{
MyStruct myStruct = (MyStruct) value;
return String.Format("{0},{1}", myStruct.i, myStruct.j);
}
else
{
return base.ConvertTo(context, culture, value, destinationType);
}
}
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
if (destinationType == typeof(String))
{
return true;
}
else
{
return base.CanConvertTo(context, destinationType);
}
}
}
[Serializable]
public struct MyStruct
{
public byte i;
public byte j;
public MyStruct(byte a, byte b)
{
i = a;
j = b;
}
}
private byte _i;
Public byte i
{
get {return _i;}
set { _i=value;}
}
private byte _j;
Public byte j
{
get {return _j;}
set { _j=value;}
}