温馨提示×

温馨提示×

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

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

ASP.NET中TypeConverter的作用是什么

发布时间:2021-07-15 15:11:45 来源:亿速云 阅读:145 作者:Leah 栏目:编程语言

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(true, ResponseFormatMode.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。代码如下:

  1. public class EmployeeConverter : TypeConverter  

  2. {  

  3. public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)  

  4. {  

  5. if (sourceType == typeof(String))  

  6. {  

  7. return true;  

  8. }  

  9.  

  10. return false;  

  11. }  

  12.  

  13. public override object ConvertFrom(ITypeDescriptorContext context, 
    CultureInfo culture, object value)  

  14. {  

  15. IDictionary<string, object> dictObj =  

  16. JavaScriptObjectDeserializer.DeserializeDictionary(value.ToString());  

  17.  

  18. Employee emp = new Employee();  

  19. emp.Name = dictObj["Name"].ToString();  

  20. emp.Age = (int)dictObj["Age"];  

  21.  

  22. return emp;  

  23. }  

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

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

  1. <html xmlns="http://www.w3.org/1999/xhtml" > 

  2. <head> 

  3. <title>Convert Primitive Object using Customized TypeConverter</title> 

  4. <script language="javascript"> 

  5. function submitEmployee()  

  6. {  

  7. var emp = new Object();  

  8. emp.Name = $("txtName").value;  

  9. emp.Age = parseInt($("txtAge").value, 10);  

  10.  

  11. var serializedEmp = Sys.Serialization.JSON.serialize(emp);  

  12. var url = "HttpGetEmployeeService.asmx?mn=SubmitEmployee&employee=" + 
    encodeURI(serializedEmp);  

  13. window.open(url);  

  14. }  

  15. </script> 

  16. </head> 

  17. <body style="font-family:Verdana; font-size:13px;"> 

  18. <form runat="server"> 

  19. <atlas:ScriptManager ID="ScriptManager1" runat="server" /> 

  20.  

  21. <div>Name:<input type="text" id="txtName" /></div> 

  22. <div>Age:<input type="text" id="txtAge" /></div> 

  23. <input type="button" value="Submit" onclick="submitEmployee();" /> 

  24. </form> 

  25. </body> 

  26. </html> 

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

向AI问一下细节

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

AI