C# 匿名对象动弹添加属性

Dictionary<string, object> temp = new Dictionary<string, object>();
if (!string.IsNullOrEmpty(ProjectName))
{
temp.Add("ProjectName", ProjectName);

}
if (!string.IsNullOrEmpty(Department))
{
temp.Add("Department", Department);

}
if (Month1>0)
{
temp.Add("Month1", Month1);

}
if (Month2 > 0)
{
temp.Add("Month2", Month2);

}
dynamic obj = new System.Dynamic.ExpandoObject();

foreach (KeyValuePair<string, object> item in temp)
{
((IDictionary<string, object>)obj).Add(item.Key, item.Value);
}

 

最后obj 就是所要的

posted @ 2019-07-01 13:59  fuwanli  阅读(455)  评论(0编辑  收藏  举报