码迷,mamicode.com
首页 > 其他好文 > 详细

ExpandoObject与DynamicObject的使用

时间:2019-01-04 12:22:06      阅读:237      评论:0      收藏:0      [点我收藏+]

标签:class   void   result   rri   EAP   add   ogr   names   get   

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的使用

标签:class   void   result   rri   EAP   add   ogr   names   get   

原文地址:https://www.cnblogs.com/chenyishi/p/10218296.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!