使用 Daynamic 动态添加属性

 

所谓的Dynamic 动态类型,页面也要是动态的(强撸)

很简单的 直接上代码:

 //案例一

DynamicpersonCollection = new ObservableCollection();

for (var i = 0; i < 10; i++) {

dynamic p = new ExpandoObject();

((IDictionary<string, object>)p).Add("Name", "22");

((IDictionary<string, object>)p).Add("Age", "cc");

DynamicpersonCollection.Add(p);

 }

 

//案例二

 

var DynamicpersonCollection = new ObservableCollection<dynamic>();
for (int i = 0; i < 2; i++)
{
string s = "你好";
dynamic p = new ExpandoObject();
p.aa = "ss";
((IDictionary<string, object>)p).Add("GroupName" + (i + 2).ToString(), s);
DynamicpersonCollection.Add(p);
}

 

案例三

public class NurseScheduleStatisticsModel : DynamicObject
    {
        public string EmpName { get; set; }
        public string TotalHour { get; set; }
        public string TotalWork { get; set; }

        Dictionary<string, object> Properties = new Dictionary<string, object>();

        public override bool TrySetMember(SetMemberBinder binder, object value)
        {
            if (!Properties.Keys.Contains(binder.Name))
            {
         //在此可以做一些小动作

          //if (binder.Name == "Col")
          //  Properties.Add(binder.Name + (Properties.Count), value.ToString());
          //else
          //  Properties.Add(binder.Name, value.ToString());

                Properties.Add(binder.Name, value.ToString());
            }
            return true;
        }
        public override bool TryGetMember(GetMemberBinder binder, out object result)
        {
            return Properties.TryGetValue(binder.Name, out result);
        }
    }
http://www.cnblogs.com/maomiyouai/p/3594132.html

 

posted @ 2015-10-26 14:53  骚年丶勿忘初心。  阅读(509)  评论(0编辑  收藏  举报