标签:dict register lis readonly bin mes 序列化 converter nta
Nancy是一个轻量级的webapi框架,在用Nancy框架的时候,我们经常要获取到接口的所有动态参数值,
但是Nancy的Request.Form不能够直接转成NameValueCollection。
在这里,我先将Request.Form序列化成json格式字符串,在反序列化成List集合,来进行获取所有的key,
从而用key获取到所有的参数的值,存入字典类中。
下面插入方法代码:
public Dictionary<string, string> GetRequestPost() { List<string> list = JsonDynamicUtil.ToList(JsonDynamicUtil.ToJson(Request.Form)); Dictionary<string, string> dic = new Dictionary<string, string>(); foreach (var iten in list) { dic.Add(iten, Request.Form[iten]); } return dic; }
下面是需要用到的序列化对象操作类:
using System; using System.Collections; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Dynamic; using System.Linq; using System.Web; using System.Web.Script.Serialization; namespace Base { public class JsonDynamicUtil { public static dynamic ToDynamic(string json) { JavaScriptSerializer jss = new JavaScriptSerializer(); jss.RegisterConverters(new JavaScriptConverter[] { new DynamicJsonConverter() }); dynamic dy = jss.Deserialize(json, typeof(object)) as dynamic; return dy; } public static string ToJson(dynamic d) { JavaScriptSerializer oSerializer = new JavaScriptSerializer(); oSerializer.RegisterConverters(new JavaScriptConverter[] { new ExpandoJSONConverter() }); string json = oSerializer.Serialize(d); return json; } public static List<string> ToList(string json) { JavaScriptSerializer jss = new JavaScriptSerializer(); jss.RegisterConverters(new JavaScriptConverter[] { new DynamicJsonConverter() }); List<string> dy = jss.Deserialize(json, typeof(List<string>)) as List<string>; return dy; } } public class DynamicJsonConverter : JavaScriptConverter { public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer) { if (dictionary == null) throw new ArgumentNullException("dictionary"); if (type == typeof(object)) { return new DynamicJsonObject(dictionary); } return null; } public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer) { throw new NotImplementedException(); } public override IEnumerable<Type> SupportedTypes { get { return new ReadOnlyCollection<Type>(new List<Type>(new Type[] { typeof(object) })); } } } public class DynamicJsonObject : DynamicObject { private IDictionary<string, object> Dictionary { get; set; } public DynamicJsonObject(IDictionary<string, object> dictionary) { this.Dictionary = dictionary; } public override bool TryGetMember(GetMemberBinder binder, out object result) { result = this.Dictionary[binder.Name]; if (result is IDictionary<string, object>) { result = new DynamicJsonObject(result as IDictionary<string, object>); } else if (result is ArrayList && (result as ArrayList) is IDictionary<string, object>) { result = new List<DynamicJsonObject>((result as ArrayList).ToArray().Select(x => new DynamicJsonObject(x as IDictionary<string, object>))); } else if (result is ArrayList) { result = new List<object>((result as ArrayList).ToArray()); } return this.Dictionary.ContainsKey(binder.Name); } } public class ExpandoJSONConverter : JavaScriptConverter { public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer) { throw new NotImplementedException(); } public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer) { var result = new Dictionary<string, object>(); var dictionary = obj as IDictionary<string, object>; foreach (var item in dictionary) result.Add(item.Key, item.Value); return result; } public override IEnumerable<Type> SupportedTypes { get { return new ReadOnlyCollection<Type>(new Type[] { typeof(System.Dynamic.ExpandoObject) }); } } } }
如果你有更好的获取方式,希望可以留言指点一下,谢谢
参考:
https://blog.csdn.net/jingvencn/article/details/41868479
标签:dict register lis readonly bin mes 序列化 converter nta
原文地址:https://www.cnblogs.com/luol/p/10217519.html