本文整理汇总了C#中System.Web.Script.Serialization.JavaScriptConverter类的典型用法代码示例。如果您正苦于以下问题:C# JavaScriptConverter类的具体用法?C# JavaScriptConverter怎么用?C# JavaScriptConverter使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
JavaScriptConverter类属于System.Web.Script.Serialization命名空间,在下文中一共展示了JavaScriptConverter类的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的C#代码示例。
示例1: Deserialize
//引入命名空间
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Web.UI.WebControls;
using System.Collections;
namespace System.Web.Script.Serialization.CS
{
public class ListItemCollectionConverter : JavaScriptConverter
{
public override IEnumerable<Type> SupportedTypes
{
//Define the ListItemCollection as a supported type.
get { return new ReadOnlyCollection<Type>(new List<Type>(new Type[] { typeof(ListItemCollection) })); }
}
public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer)
{
ListItemCollection listType = obj as ListItemCollection;
if (listType != null)
{
// Create the representation.
Dictionary<string, object> result = new Dictionary<string, object>();
ArrayList itemsList = new ArrayList();
foreach (ListItem item in listType)
{
//Add each entry to the dictionary.
Dictionary<string, object> listDict = new Dictionary<string, object>();
listDict.Add("Value", item.Value);
listDict.Add("Text", item.Text);
itemsList.Add(listDict);
}
result["List"] = itemsList;
return result;
}
return new Dictionary<string, object>();
}
public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer)
{
if (dictionary == null)
throw new ArgumentNullException("dictionary");
if (type == typeof(ListItemCollection))
{
// Create the instance to deserialize into.
ListItemCollection list = new ListItemCollection();
// Deserialize the ListItemCollection's items.
ArrayList itemsList = (ArrayList)dictionary["List"];
for (int i=0; i<itemsList.Count; i++)
list.Add(serializer.ConvertToType<ListItem>(itemsList[i]));
return list;
}
return null;
}
}
}
开发者ID:.NET开发者,项目名称:System.Web.Script.Serialization,代码行数:63,代码来源:JavaScriptConverter
注:本文中的System.Web.Script.Serialization.JavaScriptConverter类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论