您的足迹:首页 > 未分类 >dynamic的操作

dynamic的操作

dynamic的操作

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"))

            }


本博客所有文章如无特别注明均为原创。作者:cc复制或转载请以超链接形式注明转自 我的博客
原文地址《dynamic的操作

相关推荐

发表评论

路人甲 表情
Ctrl+Enter快速提交

网友评论(0)