public delegate object MyDelegate(dynamic Sender, params object[] PMs);
public class DelegateObj
{
private MyDelegate _delegate;
public MyDelegate CallMethod
{
get { return _delegate; }
}
private DelegateObj(MyDelegate D)
{
_delegate = D;
}
/// <summary>
/// 构造委托对象,让它看起来有点javascript定义的味道.
/// </summary>
/// <param name="D"></param>
/// <returns></returns>
public static DelegateObj Function(MyDelegate D)
{
return new DelegateObj(D);
}
}
public class DynObj : DynamicObject
{
//保存对象动态定义的属性值
private Dictionary<string, object> _values;
public DynObj()
{
_values = new Dictionary<string, object>();
}
/// <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 DelegateObj;
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);
}
}
dynamic theObj = new DynObj();
theObj.aaa = "this is a test";//动态属性
//动态方法,这里不能没法定义参数,调用的时候可以是任意多参数,具体参数类型和含义就只能自己去小心处理了.
theObj.show = DelegateObj.Function((s, pms) =>
{
if (pms != null && pms.Length > 0)
{
MessageBox.Show(pms[0].ToString() + ":" + s.aaa);
}
else
{
MessageBox.Show(s.aaa);
}
return null;
}
);
theObj.show("hello");
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有