当前位置: 首页>>代码示例>>C#>>正文


C# ExpandoObject.DoBIncrement方法代码示例

本文整理汇总了C#中System.Dynamic.ExpandoObject.DoBIncrement方法的典型用法代码示例。如果您正苦于以下问题:C# ExpandoObject.DoBIncrement方法的具体用法?C# ExpandoObject.DoBIncrement怎么用?C# ExpandoObject.DoBIncrement使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Dynamic.ExpandoObject的用法示例。


在下文中一共展示了ExpandoObject.DoBIncrement方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Execute

        public override void Execute()
        {
            Console.WriteLine("dynamic item = new ExpandoObject();");
            dynamic item = new ExpandoObject();

            // we can add properties ad-hoc
            Console.WriteLine("item.Name = \"Billy\";");
            item.Name = "Billy";

            Console.WriteLine("item.DoB = new DateTime(1999, 02, 22);");
            item.DoB = new DateTime(1999, 02, 22);

            // and add methods
            item.DoBIncrement = (Action)(() => { item.DoB = item.DoB.AddYears(+1); });

            // and call them
            item.DoBIncrement();

            Console.WriteLine("item.DoB.ToString()");
            Console.WriteLine(item.DoB.ToString());

            Console.WriteLine("item.Name");
            Console.WriteLine(item.Name);

            // we can also change the properties radically
            Console.WriteLine("item.Name = 1.2;");
            item.Name = 1.2;
            Console.WriteLine("item.Name.GetType().ToString()");
            Console.WriteLine(item.Name.GetType().ToString());

            // we can pass it as a parameter (as long as the dynamic keyword is used)
            ShowName(item);

            // we can also add events (but we must first initialise them with null!)
            item.SomethingHappened = null;
            item.SomethingHappened += new EventHandler<NameEventArgs>(NowWeNowThatSomethingHappened);
            // also implement the handler in a fairly normal way
            item.OnSomethingHappened =  (Action)(() =>
                {
                    if (item.SomethingHappened != null)
                    {
                        item.SomethingHappened(item, new NameEventArgs(item.Name));
                    }
                });

            // and hook up to them externally...
            item.Name = "Billy Bunter";
            item.OnSomethingHappened();

            // we can also list the properties
            Console.WriteLine("Properties, Methods, etc... are stored as a IDictionary<String,Object> :");
            foreach (var property in (IDictionary<String, Object>)item)
            {
                Console.WriteLine("\tkey: " + property.Key + ", value: " + property.Value);
            }

            // throws a LANGUAGE-SPECIFIC error when retrieving a non-existant property!
            // ?? presumably a VB.NET authored DLL would throw a _different_ error? ??
            try
            {
                Console.WriteLine("item.Salary");
                Console.WriteLine(item.Salary);
            }
            catch(Microsoft.CSharp.RuntimeBinder.RuntimeBinderException e)
            {
                Console.WriteLine(e.Message);
            }

            Console.ForegroundColor = ConsoleColor.Cyan;
            // VB.NET has had late-binding dynamic-like behaviour - but the behaviours/exceptions are different
            // keyword "dynamic" is a C# ONLY feature
            Console.WriteLine("(* VB.NET *)");
            Console.WriteLine("ExpandoTest test = new ExpandoTest();");
            ExpandoTest test = new ExpandoTest();
            try
            {
                Console.WriteLine("test.NonExistantProperty();");
                Console.WriteLine(test.NonExistantProperty());
            }
            catch (MissingMemberException e)
            {
                Console.WriteLine(e.Message);
            }
            Console.ResetColor();
        }
开发者ID:predication,项目名称:experiment,代码行数:85,代码来源:DynamicExample.cs


注:本文中的System.Dynamic.ExpandoObject.DoBIncrement方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。