92 lines
2.2 KiB
C#
92 lines
2.2 KiB
C#
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<T_PF_PARAM> _dataDictionary = null;
|
|
private object _entityBase = null;
|
|
// Methods
|
|
public ParamDynamicDictionary(ICollection<T_PF_PARAM> 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<string> GetDynamicMemberNames()
|
|
{
|
|
Dictionary<string, string> dic = new Dictionary<string, string>();
|
|
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;
|
|
}
|
|
}
|
|
}
|