using System; using System.Collections.Generic; using System.Dynamic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace APT.Infrastructure.Core { public class ParamDynamicDictionary : DynamicObject { // Fields private ICollection _dataDictionary = null; private object _entityBase = null; // Methods public ParamDynamicDictionary(ICollection dataDictionary, object entityBase) { if (dataDictionary == null||entityBase==null) throw new Exception("参数异常"); _dataDictionary = dataDictionary; _entityBase = entityBase; } public string this[string key] { get { string obj = null; if (_dataDictionary != null && _dataDictionary.Count > 0) { var param = _dataDictionary.FirstOrDefault(t => string.Compare(t.CODE, key, true) == 0); if (param != null && !string.IsNullOrEmpty(param.VALUE)) obj = param.VALUE.Trim(); } return obj; } set { if (_dataDictionary != null) { var param= _dataDictionary.FirstOrDefault(t => string.Compare(t.CODE, key, true) == 0); if (param == null) { param = new T_PF_PARAM(); param.CODE = key; var pro= _entityBase.GetType().GetProperty("ID"); if (pro != null) { var idObj= pro.GetValue(_entityBase, null); if (idObj != null) param.ENTITY_ID = new Guid(idObj.ToString()); } _dataDictionary.Add(param); } if (value == null) param.VALUE = null; else param.VALUE = value.Trim(); } } } public override IEnumerable GetDynamicMemberNames() { Dictionary dic = new Dictionary(); if (_dataDictionary != null && _dataDictionary.Count > 0) { foreach (var item in _dataDictionary) dic[item.CODE] = item.VALUE; } return dic.Keys; } public override bool TryGetMember(GetMemberBinder binder, out object result) { result = this[binder.Name]; if (result != null) result = result.ToString().Trim(); return true; } public override bool TrySetMember(SetMemberBinder binder, object value) { if (value == null) this[binder.Name] = null; else { this[binder.Name] = value.ToString().Trim(); } return true; } } }