c# dynamic动态类型和匿名类
2021-04-22 02:26
标签:添加 each 动态 from 不可 pos 属性 value enc dynamic类型 动态添加字段 枚举该对象所有成员 c# dynamic动态类型和匿名类 标签:添加 each 动态 from 不可 pos 属性 value enc 原文地址:https://www.cnblogs.com/hnsongbiao/p/8250956.html
简单示例dynamic expando = new System.Dynamic.ExpandoObject(); //动态类型字段 可读可写
expando.Id = 1;
expando.Name = "Test";
string json = Utils.ConvertJson.JsonEncode(expando); //输出{Id:1,Name:‘Test‘}
Liststring> fieldList = new Liststring>() { "Name","Age","Sex"}; //From config or db
dynamic dobj = new System.Dynamic.ExpandoObject();
var dic = (IDictionarystring, object>)dobj;
foreach (var fieldItem in fieldList)
{
dic[fieldItem] = "set "+fieldItem+" value"; /*实现类似js里的 动态添加属性的功能
var obj={};
var field="Id";
eval("obj."+field+"=1");
alert(obj.Id); //1 */
}
var val = dobj.Name; //“set Name value”
var val1=dobj.Age;//”set Age value“
foreach (var fieldItem in (IDictionary
文章标题:c# dynamic动态类型和匿名类
文章链接:http://soscw.com/index.php/essay/77870.html