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

动态对象DynamicObject实现让字典书写更优美

时间:2015-08-25 11:55:26      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:

我想用过MVC的人都知道一件事,viewbag的值是存储在viewdata里面的。

 

先看以下组图:

技术分享

技术分享技术分享技术分享

技术分享

看了上图是不是感觉很神奇,Dictionary<string, object>也可以这样使用~

实现主要继承了DynamicObject

接下来直接贴代码:

/// <summary>
    /// 动态对象
    /// </summary>
    public class VarObject : DynamicObject
    {
        /// <summary>
        /// 数据字段
        /// </summary>
        private Dictionary<string, object> ViewData = new Dictionary<string, object>();

        /// <summary>
        /// 调用 varo(); 时执行
        /// dynamic varo = new VarObject();
        /// </summary>
        /// <param name="binder"></param>
        /// <param name="args"></param>
        /// <param name="result"></param>
        /// <returns></returns>
        public override bool TryInvoke(InvokeBinder binder, object[] args, out object result)
        {
            return base.TryInvoke(binder, args, out result);
        }

        /// <summary>
        /// 调用 varo.Method(); 时执行
        /// dynamic varo = new VarObject();
        /// </summary>
        /// <param name="binder"></param>
        /// <param name="args"></param>
        /// <param name="result"></param>
        /// <returns></returns>
        public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result)
        {
            return base.TryInvokeMember(binder, args, out result);
        }

        /// <summary>
        /// 调用 varo + varo; 时执行
        /// dynamic varo = new VarObject();
        /// </summary>
        /// <param name="binder"></param>
        /// <param name="arg"></param>
        /// <param name="result"></param>
        /// <returns></returns>
        public override bool TryBinaryOperation(BinaryOperationBinder binder, object arg, out object result)
        {
            return base.TryBinaryOperation(binder, arg, out result);
        }

        /// <summary>
        /// 调用 varo++; 时执行
        /// dynamic varo = new VarObject();
        /// </summary>
        /// <param name="binder"></param>
        /// <param name="result"></param>
        /// <returns></returns>
        public override bool TryUnaryOperation(UnaryOperationBinder binder, out object result)
        {
            return base.TryUnaryOperation(binder, out result);
        }

        /// <summary>
        /// 调用 varo["key"]; 时执行
        /// dynamic varo = new VarObject();
        /// </summary>
        /// <param name="binder"></param>
        /// <param name="indexes"></param>
        /// <param name="result"></param>
        /// <returns></returns>
        public override bool TryGetIndex(GetIndexBinder binder, object[] indexes, out object result)
        {
            if (indexes == null || indexes.Length != 1)
            {
                throw new ArgumentException("indexes");
            }

            result = null;
            string key = indexes[0] as string;
            if (key != null)
            {
                result = ViewData[key];
            }
            else
            {
                throw new ArgumentException("indexes");
            }
            return true;
        }

        /// <summary>
        /// 调用 varo["key"] = "value"; 时执行
        /// dynamic varo = new VarObject();
        /// </summary>
        /// <param name="binder"></param>
        /// <param name="indexes"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public override bool TrySetIndex(SetIndexBinder binder, object[] indexes, object value)
        {
            if (indexes == null || indexes.Length != 1)
            {
                throw new ArgumentException("indexes");
            }

            string key = indexes[0] as string;
            if (key != null)
            {
                ViewData[key] = value;
                return true;
            }
            else
            {
                throw new ArgumentException("indexes");
            }
        }

        /// <summary>
        /// 获取所有key
        /// </summary>
        /// <returns></returns>
        public override IEnumerable<string> GetDynamicMemberNames()
        {
            return ViewData.Keys;
        }

        /// <summary>
        /// 调用 varo.key; 时执行
        /// dynamic varo = new VarObject();
        /// </summary>
        /// <param name="binder"></param>
        /// <param name="result"></param>
        /// <returns></returns>
        public override bool TryGetMember(GetMemberBinder binder, out object result)
        {
            result = ViewData[binder.Name];
            return true;
        }

        /// <summary>
        /// 调用 varo.key = "value"; 时执行
        /// dynamic varo = new VarObject();
        /// </summary>
        /// <param name="binder"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public override bool TrySetMember(SetMemberBinder binder, object value)
        {
            ViewData[binder.Name] = value;
            return true;
        }
    }

运行代码:

    class Program
    {

        static void Main(string[] args)
        {
            dynamic varo = new VarObject();

            varo.a = "value";
           
            varo.b = "2";

            varo["c"] = 3;

            var d = varo++;

            var e = varo + varo;

            varo(); 

            varo.Method(); 

            foreach(var v in varo.GetDynamicMemberNames())
            {
                Console.WriteLine(v); 
            }
           
            Console.WriteLine("done."); 
            Console.ReadKey();
        }
    }

 

动态对象DynamicObject实现让字典书写更优美

标签:

原文地址:http://www.cnblogs.com/ulon/p/4756695.html

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