ExpandoObject与DynamicObject的使用
Posted chenyishi
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ExpandoObject与DynamicObject的使用相关的知识,希望对你有一定的参考价值。
using ImpromptuInterface; using System; using System.Dynamic; namespace ConsoleApp2 { class Program { static void Main(string[] args) { dynamic expando = new ExpandoObject(); expando.name = "cys"; expando.Add = new Func<int, int>(i=> { return i; }); var expando_result = expando.Add(1); Console.WriteLine(expando.GetType().Name); dynamic dobject = new BB { name="aaa" }; var dobject_result = dobject.ADD(); dobject.Name = "cys"; Console.WriteLine(dobject.Name); Console.WriteLine(dobject.GetType().Name); } } public class BB : DynamicObject { public string name { get; set; } public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result) { result = "a"; return true; } public override bool TryGetMember(GetMemberBinder binder, out object result) { result = name; return true; } public override bool TrySetMember(SetMemberBinder binder, object value) { name = value.ToString(); return true; } } }
以上是关于ExpandoObject与DynamicObject的使用的主要内容,如果未能解决你的问题,请参考以下文章
ExpandoObject、DynamicObject 和 dynamic 的区别
如何使用 ServiceStack JsonSerializer 序列化 ExpandoObject?