自定义动态对象-创新互联
1,自定义动态对象需要继承DynamicObject类

2,可根据需要,重写不同的DynamicObject方法
-----------------------------------------------------DynamicClass.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Dynamic;
namespace ConsoleApplication3
{
public class DynamicClass:DynamicObject
{
Dictionary _dynamicData = new Dictionary();
///
/// 为获取成员值的操作提供实现
///
///
///
///
public override bool TryGetMember(GetMemberBinder binder, out object result)
{
bool success = false;
result = null;
if (_dynamicData.ContainsKey(binder.Name))
{
result = _dynamicData[binder.Name];
success = true;
}
else
{
result = "Property Not Found";
success = false;
}
return success;
}
///
/// 为设置成员值的操作提供实现
///
///
///
///
public override bool TrySetMember(SetMemberBinder binder, object value)
{
_dynamicData[binder.Name]= value;
return true;
}
}
} -----------------------------------------------------主程序
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Dynamic;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
dynamic dyn = new DynamicClass();
dyn.name = "a";
dyn.aa = new Func(r => r);
dyn.name = "b";
Console.WriteLine(dyn.name+"|"+ dyn.aa("b")); //输出:b|b
Console.ReadKey();
}
}
} 创新互联www.cdcxhl.cn,专业提供香港、美国云服务器,动态BGP最优骨干路由自动选择,持续稳定高效的网络助力业务部署。公司持有工信部办法的idc、isp许可证, 机房独有T级流量清洗系统配攻击溯源,准确进行流量调度,确保服务器高可用性。佳节活动现已开启,新人活动云服务器买多久送多久。
当前名称:自定义动态对象-创新互联
当前地址:http://www.jxjierui.cn/article/coopoc.html


咨询
建站咨询
