public class RONT_EXAMINE_VIEW_MODEL : DynamicObject { //保存对象动态定义的属性值 private Dictionary<string, object> _values; public RONT_EXAMINE_VIEW_MODEL() { _values = new Dictionary<string, object>(); } public Dictionary<string, object> Values { get { return _values; } } /// <summary> /// 获取属性值 /// </summary> /// <param name="propertyName"></param> /// <returns></returns> public object GetPropertyValue(string propertyName) { if (_values.ContainsKey(propertyName) == true) { return _values[propertyName]; } return null; } /// <summary> /// 设置属性值 /// </summary> /// <param name="propertyName"></param> /// <param name="value"></param> public void SetPropertyValue(string propertyName, object value) { if (_values.ContainsKey(propertyName) == true) { _values[propertyName] = value; } else { _values.Add(propertyName, value); } } /// <summary> /// 实现动态对象属性成员访问的方法,得到返回指定属性的值 /// </summary> /// <param name="binder"></param> /// <param name="result"></param> /// <returns></returns> public override bool TryGetMember(GetMemberBinder binder, out object result) { result = GetPropertyValue(binder.Name); return result == null ? false : true; } /// <summary> /// 实现动态对象属性值设置的方法。 /// </summary> /// <param name="binder"></param> /// <param name="value"></param> /// <returns></returns> public override bool TrySetMember(SetMemberBinder binder, object value) { SetPropertyValue(binder.Name, value); return true; } /// <summary> /// 动态对象动态方法调用时执行的实际代码 /// </summary> /// <param name="binder"></param> /// <param name="args"></param> /// <param name="result"></param> /// <returns></returns> public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result) { var theDelegateObj = GetPropertyValue(binder.Name) as DelegateObjs; if (theDelegateObj == null || theDelegateObj.CallMethod == null) { result = null; return false; } result = theDelegateObj.CallMethod(this, args); return true; } public override bool TryInvoke(InvokeBinder binder, object[] args, out object result) { return base.TryInvoke(binder, args, out result); } } public delegate object MyDelegates(dynamic Sender, params object[] PMs); public class DelegateObjs { private MyDelegates _delegate; public MyDelegates CallMethod { get { return _delegate; } } private DelegateObjs(MyDelegates D) { _delegate = D; } /// <summary> /// 构造委托对象,让它看起来有点javascript定义的味道. /// </summary> /// <param name="D"></param> /// <returns></returns> public static DelegateObjs Function(MyDelegates D) { return new DelegateObjs(D); } }
dynamic view = new RONT_EXAMINE_VIEW_MODEL(); view.AC_VALUE = item.AC_VALUE; view.CHECK_TOOL = item.CHECK_TOOL; view.CHECK_TYPE = item.CHECK_TYPE; view.DUTY_DEPARTMENT = item.DUTY_DEPARTMENT; view.FAIL_DESC = item.FAIL_DESC; view.FEATURES = item.FEATURES; view.FEATURES_LEVEL = item.FEATURES_LEVEL; view.FIX_NUM = item.FIX_NUM; view.ID = item.ID; view.INSPECTION_LEVEL = item.INSPECTION_LEVEL; view.INSPECTION_RESULT = item.INSPECTION_RESULT; view.IS_INSPECTOR = item.IS_INSPECTOR; view.ISQUAL = item.ISQUAL; view.ITEM_NAME = item.ITEM_NAME; view.ITEM_TYPE = item.ITEM_TYPE; view.OFFSET_VALUE_LOW = item.OFFSET_VALUE_LOW; view.OFFSET_VALUE_UP = item.OFFSET_VALUE_UP; view.RE_VALUE = item.RE_VALUE; view.REFERENCE_VALUE = item.REFERENCE_VALUE; view.ROW_INDEX = item.ROW_INDEX; view.SAMPLE_INSPECTION_ID = item.SAMPLE_INSPECTION_ID; view.SEQ = item.SEQ; view.STANDARD_VALUE = item.STANDARD_VALUE; view.STATE = item.STATE; view.STATUS = item.STATUS; view.TECH_REQUIREMENT = item.TECH_REQUIREMENT; view.UNIT = item.UNIT; view.RowOrder = item.ROW_INDEX; foreach (var extend in extendColumns.FindAll(e => e.DETAIL_ID == item.ID)) { view.SetPropertyValue(extend.COLUMN_NAME.Trim(), extend.COLUMN_VALUE); } retObjs.Add(view);