自定义复杂类型转换器(TypeConverter)错误,请大家帮忙看看

DBXP 2010-06-17 10:26:32
请朋友们帮忙看看我的转换器问题出在哪里,谢谢各位!

我使用 System.ComponentModel.TypeConverter 实现了一个服务器控件的自定义复杂类型属性转换器,错误信息如下:
分析器错误
说明: 在分析向此请求提供服务所需资源时出错。请检查下列特定分析错误详细信息并适当地修改源文件。

分析器错误消息: 无法为“www.Class1”类型的值生成代码。试图为 mypro 生成属性值时出现此错误。

aspx页面代码如下:

<%@ 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;
}
}

}
...全文
262 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
mapserver 2010-06-18
  • 打赏
  • 举报
回复
那再给你一篇我写的文章:

http://www.cnblogs.com/mapserver/articles/444722.html

asp.net server control的复杂属性处理。
DBXP 2010-06-18
  • 打赏
  • 举报
回复
回mapserver:您给的例子在Winform下是好用的,但是在WebForm下不能正确运行,提示错误信息如下:

分析器错误消息: 无法为“ClassLibrary1.Size”类型的值生成代码。试图为 Size 生成属性值时出现此错误。
幻想多巴胺 2010-06-18
  • 打赏
  • 举报
回复
mark620 2010-06-18
  • 打赏
  • 举报
回复
断点调试。
suxujie 2010-06-18
  • 打赏
  • 举报
回复
帮顶,没分求助了,楼主给点分!谢谢!
mapserver 2010-06-17
  • 打赏
  • 举报
回复
给你篇我原来写的文章吧,讲TypeConverterAttribute的。

http://www.cnblogs.com/mapserver/articles/353722.html
永生天地 2010-06-17
  • 打赏
  • 举报
回复
ie状态栏上的错误,双击打开看看错误所在之处
DBXP 2010-06-17
  • 打赏
  • 举报
回复
回 xray2005:F9+F10调试中没有发现错误,最后页面上才冒出错误信息。
xray2005 2010-06-17
  • 打赏
  • 举报
回复
具体错什么地方呢?

你F9设置断点,调试看看

62,041

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧