62,041
社区成员
发帖
与我相关
我的任务
分享
<%@ Page Language="C#" AutoEventWireup="true" %>
<%@ Register assembly="www" namespace="www" tagprefix="cc1" %>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
string s = www.String2ObjectTypeConverter.Object2String(new Class1());
this.TextBox1.Text = s;
object o = www.String2ObjectTypeConverter.String2Object(s);
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
<form id="form1" runat="server">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<cc1:WebCustomControl1 ID="WebCustomControl1" runat="server" mypro="AAEAAAD/////AQAAAAAAAAAMAgAAADp3d3csIFZlcnNpb249MC4wLjAuMCwgQ3VsdHVyZT1uZXV0cmFsLCBQdWJsaWNLZXlUb2tlbj1udWxsBQEAAAAKd3d3LkNsYXNzMQIAAAAVPG5hbWU+a19fQmFja2luZ0ZpZWxkFDxhZ2U+a19fQmFja2luZ0ZpZWxkAQAIAgAAAAYDAAAAAk9LCgAAAAs=" />
</form>
</body>
</html>
namespace www
{
[System.ComponentModel.DefaultProperty("Text")]
[System.Web.UI.ToolboxData("<{0}:WebCustomControl1 runat=server></{0}:WebCustomControl1>")]
public class WebCustomControl1 : System.Web.UI.WebControls.WebControl
{
[System.ComponentModel.TypeConverter(typeof(String2ObjectTypeConverter))]
public Class1 mypro
{
get;
set;
}
protected override void RenderContents(System.Web.UI.HtmlTextWriter output)
{
output.Write("Demo");
}
}
public class String2ObjectTypeConverter : System.ComponentModel.TypeConverter
{
/// <summary>
/// 将对象序列化为一个字符串。
/// </summary>
/// <param name="o"></param>
/// <returns></returns>
public static string Object2String(object o)
{
using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
{
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter fmt = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
fmt.Serialize(ms, o);
byte[] bytes = ms.ToArray();
return System.Convert.ToBase64String(bytes);
}
}
/// <summary>
/// 从字符串还原对象。
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static object String2Object(string str)
{
using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
{
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter fmt = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
byte[] bytes = System.Convert.FromBase64String(str);
ms.Write(bytes, 0, bytes.Length);
ms.Seek(0, System.IO.SeekOrigin.Begin);
return fmt.Deserialize(ms);
}
}
public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType)
{
if (sourceType == typeof(string))
{
return true;
}
return base.CanConvertFrom(context, sourceType);
}
public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type destinationType)
{
if (destinationType == typeof(string))
{
return true;
}
else if (destinationType == typeof(System.ComponentModel.Design.Serialization.InstanceDescriptor))
{
return true;
}
return base.CanConvertTo(context, destinationType);
}
public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
{
if (value is string)
{
return String2Object(value.ToString());
}
return base.ConvertFrom(context, culture, value);
}
public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType)
{
if (destinationType == typeof(string))
{
return Object2String(value);
}
else if (destinationType == typeof(System.ComponentModel.Design.Serialization.InstanceDescriptor) && (value is Class1))
{
return new System.ComponentModel.Design.Serialization.InstanceDescriptor(typeof(Class1).GetConstructor(new System.Type[] { typeof(string) }), new string[] { Object2String(value) });
}
return base.ConvertTo(context, culture, value, destinationType);
}
}
[System.Serializable]
[System.ComponentModel.TypeConverter(typeof(String2ObjectTypeConverter))]
public class Class1
{
public string name
{
get;
set;
}
public int age
{
get;
set;
}
public Class1()
{
this.name = "OK";
this.age = 10;
}
public Class1(string name, int age)
{
this.name = name;
this.age = age;
}
public new string ToString()
{
return this.name;
}
}
}