自定义动态对象
1,自定义动态对象需要继承DynamicObject类
成都创新互联专注为客户提供全方位的互联网综合服务,包含不限于成都做网站、成都网站制作、宁远网络推广、重庆小程序开发、宁远网络营销、宁远企业策划、宁远品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;成都创新互联为所有大学生创业者提供宁远建站搭建服务,24小时服务热线:18980820575,官方网址:www.cdcxhl.com
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(); } } }
分享标题:自定义动态对象
当前路径:http://scyingshan.cn/article/jihess.html