温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

ASP.NET中TypeConverter的作用是什么

发布时间:2021-07-15 15:11:45 阅读:176 作者:Leah 栏目:编程语言
开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

ASP.NET中TypeConverter的作用是什么,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

JavaScriptConverter类的作用是提供了开发人员自定义序列化与反序列化的能力,这一点对于操作含有循环引用的复杂对象尤其重要。这个类在RTM Release中的功能被精简了。它的方法和属性被缩减成了三个:

1. IEnumerable<Type> SupportedTypes:只读属性,返回这个Converter所有能够支持的类。

2. object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer):

这个方法的***个参数是一个字典,有朋友可能会认为这个字典和JSON字符串的表示非常的接近:由Dictionary和List嵌套而成,***端的元素为一些基本类型对象。不过事实上不是如此。ASP.NET AJAX在反序列化一个JSON字符串时,如果出现了“{ "__type" : "...", ...}” 这样的片断时,在将其转换为真正的JSON表示的Dictionary(只存在基本类型对象的Dictionary)之后,如果发现该 Dictionary存在“__type”这个Key,那么就会设法在这个时候就将它转换为__type值表示的那个类型了。也就是说, JavaScriptConverter的Deserialize方法接受到的***个参数字典中,也有可能已经是一个特殊的类型了。

第二个参数为转换的目标类型。而第三个参数,则是调用当前Deserialize方法的JavaScriptSerializer了,我们的一些反序列化操作可以委托给它执行,它已经关联好了web.config中配置的JavaScriptConverter。不过需要注意的就是,千万要避免下一步操作又没有改变地回到了当前的Deserialize方法,显然这样会出现死循环。

3. IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer):这个方法的作用相对纯粹一些,将obj对象转换为一个IDictionary<string, object>对象,在这个方法将结果返回后,ASP.NET AJAX会在这个Dictionary中添加“__type”的值,这样的话,在反序列化时也能够使用当前的JavaScriptConverter来进行相反的操作。

首先,定义一个复杂类型Employee:

[TypeConverter(typeof(EmployeeConverter))]  public class Employee  {  public string Name;  public int Age;  }

可以看到,我们使用了TypeConverterAttribute将稍后会讲解的EmployeeConverter关联到Employee上。

接着,和上一个例子一样,我们写一个支持HTTP GET访问的Web Services方法,只是参数使用了复杂类型。

[WebService(Namespace = "http://tempuri.org/")]  [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]  public class HttpGetEmployeeService  : System.Web.Services.WebService {   [WebMethod]  [WebOperation(trueResponseFormatMode.Xml)]  public XmlDocument SubmitEmployee(Employee employee)  {  XmlDocument responseDoc = new XmlDocument();  responseDoc.LoadXml(  "<?xml-stylesheet type=\"text/xsl\" href=\"Employee.xsl\"?>" +  "<Employee><Name></Name><Age></Age></Employee>");  responseDoc.SelectSingleNode("//Name").InnerText = employee.Name;  responseDoc.SelectSingleNode("//Age").InnerText = employee.Age.ToString();  return responseDoc;  }  }

然后是所需的Xslt文件:

<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/Employee"> <html> <head> <title>Thanks for your participation</title> </head> <body style="font-family:Verdana; font-size:13px;"> <h5>Here's the employee you submitted:</h5> <div> <xsl:text>Name: </xsl:text> <xsl:value-of select="Name" /> </div> <div> <xsl:text>Age: </xsl:text> <xsl:value-of select="Age" /> </div> </body> </html> </xsl:template> </xsl:stylesheet>

上面这些对于看过之前一片文章的朋友们来说应该很熟悉。接下来,我们就进入正题,定义一个EmployeeConverter。代码如下:

public class EmployeeConverter : TypeConverter  {  public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)  {  if (sourceType == typeof(String))  {  return true;  }  return false;  }  public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)  {  IDictionary<stringobject> dictObj =  JavaScriptObjectDeserializer.DeserializeDictionary(value.ToString());  Employee emp = new Employee();  emp.Name = dictObj["Name"].ToString();  emp.Age = (int)dictObj["Age"];  return emp;  }  } 

EmployeeConverter继承了TypeConverter,首先覆盖CanConvertFrom方法表明使用EmployeeConverter可以将一个String转换成另一个对象。接着在覆盖 ConvertFrom方法,将传入的value值转换为一个复杂对象Employee。这里为了方便,我们把Employee对象在客户端JOSN序列化,然后在服务器端再序列化回来,事实上,这种基础类型到复杂类型的转换,完全可以使用任何方式。

代码都非常简单,也容易理解,因此我们直接看一下使用代码。由于代码很少,就将Javascript和HTML一并贴出了:

<html xmlns="http://www.w3.org/1999/xhtml" > <head> <title>Convert Primitive Object using Customized TypeConverter</title> <script language="javascript"> function submitEmployee()  {  var emp = new Object();  emp.Name = $("txtName").value;  emp.Age = parseInt($("txtAge").value10);  var serializedEmp = Sys.Serialization.JSON.serialize(emp);  var url = "HttpGetEmployeeService.asmx?mn=SubmitEmployee&employee=" + encodeURI(serializedEmp);  window.open(url);  }  </script> </head> <body style="font-family:Verdana; font-size:13px;"> <form runat="server"> <atlas:ScriptManager ID="ScriptManager1" runat="server" /> <div>Name:<input type="text" id="txtName" /></div> <div>Age:<input type="text" id="txtAge" /></div> <input type="button" value="Submit" onclick="submitEmployee();" /> </form> </body> </html> 

看完上述内容,你们掌握ASP.NET中TypeConverter的作用是什么的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI

开发者交流群×