public class DynamicObjectAbs : System.Dynamic.DynamicObject { private Dictionary<string, object> dic = new Dictionary<string, object>(); public void SetValue(string jsonText) { JObject jo = JObject.Parse(jsonText.ToString()); var li = jo.Properties().ToList(); foreach (var arrayList in li) { string aName = arrayList.Name; string aValue = arrayList.Value.ToString(); if (dic.ContainsKey(aName)) dic[aName] = aValue; else dic.Add(aName, aValue); } } public void SetValue(string key, object value) { if (dic.ContainsKey(key)) dic[key] = value; else dic.Add(key, value); } public T GetValue<T>(string key) { if (dic.ContainsKey(key)) return (T)dic[key]; else return default(T); } }
使用的例子
List<dynamic> listDynamic = new List<dynamic>(); foreach (var item in DynamicObject) { dynamic New = new DynamicObjectAbs(); New.SetValue(Convert.ToString(item)); listDynamic.Add(New); } ConfigHelper configHelper = new ConfigHelper(cb_Select.Text); List<ConvertEntity> configs = configHelper.GetAllConfig(); List<dynamic> listNewDynamic = new List<dynamic>(); foreach (var item in listDynamic) { configs.Where(s => s.Name == item.GetValue<string>("id")) }
文章评论