1,自定義動態(tài)對象需要繼承DynamicObject類
成都創(chuàng)新互聯(lián)專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務,包含不限于成都做網(wǎng)站、成都網(wǎng)站制作、寧遠網(wǎng)絡推廣、重慶小程序開發(fā)、寧遠網(wǎng)絡營銷、寧遠企業(yè)策劃、寧遠品牌公關、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運營等,從售前售中售后,我們都將竭誠為您服務,您的肯定,是我們最大的嘉獎;成都創(chuàng)新互聯(lián)為所有大學生創(chuàng)業(yè)者提供寧遠建站搭建服務,24小時服務熱線:18980820575,官方網(wǎng)址:www.cdcxhl.com
2,可根據(jù)需要,重寫不同的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 (); /// /// 為獲取成員值的操作提供實現(xiàn) /// /// /// ///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; } /// /// 為設置成員值的操作提供實現(xiàn) /// /// /// ///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(); } } }