zoukankan      html  css  js  c++  java
  • C#新特性, dynamic, ExpandObject

    http://msdn.microsoft.com/en-us/library/system.dynamic.expandoobject.aspx

    using System;
    using System.Collections.Generic;
    using System.Dynamic;
    using System.Linq;
    using System.Text;
    
    namespace TestCSharp
    {
        class ExampleClass
        {
            public ExampleClass() { }
            public ExampleClass(int v) { }
    
            public void exampleMethod1(int i) {
                Console.WriteLine(i);
            }
    
            public void exampleMethod2(string str) {
                Console.WriteLine(str);
            }
        }
    
        class Program
        {
            static void Main(string[] args)
            {
    
                ExampleClass ec = new ExampleClass();
                // The following call to exampleMethod1 causes a compiler error 
                // if exampleMethod1 has only one parameter. Uncomment the line
                // to see the error.
                //ec.exampleMethod1(10, 4);
    
                dynamic dynamic_ec = new ExampleClass();
                // The following line is not identified as an error by the
                // compiler, but it causes a run-time exception.
                dynamic_ec.exampleMethod1(10);
    
                // The following calls also do not cause compiler errors, whether 
                // appropriate methods exist or not.
                dynamic_ec.exampleMethod2("some argument");
                //dynamic_ec.nonexistentMethod();
    
                dynamic i = 10;
                ++i;
                
                Console.WriteLine(i+i);
    
                
                dynamic sampleObject = new ExpandoObject();
    
                // Create a new event and initialize it with null.
                sampleObject.sampleEvent = null;
    
                // Add an event handler.
                sampleObject.sampleEvent += new EventHandler(SampleHandler);
    
                // Raise an event for testing purposes.
                sampleObject.sampleEvent(sampleObject, new EventArgs());
                sampleObject.name = "ww";
                sampleObject.age = 23;
                Console.WriteLine("name={0},age={1}", sampleObject.name, sampleObject.age);
                ToStr(sampleObject);
               
                Console.ReadKey();
            }
            // Event handler.
            static void SampleHandler(object sender, EventArgs e)
            {
                Console.WriteLine("SampleHandler for {0} event", sender);
            }
    
            static void ToStr(dynamic sampleObject)
            {
                Console.WriteLine("ToStr:name={0},age={1}", sampleObject.name, sampleObject.age);
            }
        }
    }
    

      

  • 相关阅读:
    python学习笔记(五)---sublime text 多行代码注释快捷键
    python学习笔记(四)---python不能输出中文问题
    python学习笔记(三)---python关键字及其用法
    python学习笔记(二)---编辑工具sublimeText3运行python
    python学习笔记(一)---python下载以及环境的安装
    idea上查看本文件svn修改的历史版本
    关于app
    Git 上传文件到 码云 gitee
    Vue 图片引入
    Eslint 规则说明
  • 原文地址:https://www.cnblogs.com/wucg/p/3852501.html
Copyright © 2011-2022 走看看