Visual C# 2010 Recipes A Problem-Solution Approach 代码4——创建ExpandoObject Dynamic Type
2010-06-14 22:43
411 查看
此代码具有明显的动态语言风格,可以在此感叹一下C#4.0在动态语言特性上的巨大增强
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Dynamic; namespace Apress.VisualCSharpRecipes.Chapter01 { public class Recipe01_19 { static void Main(string[] args) { // create the expando dynamic expando = new ExpandoObject(); expando.Name = "Joe Smith"; expando.Age = 42; expando.Family = new ExpandoObject(); expando.Family.Father = "Pa Smith"; expando.Family.Mother = "Ma Smith"; expando.Family.Brother = "Pete Smith"; // access the members of the dynamic type Console.WriteLine("Name: {0}", expando.Name); Console.WriteLine("Age: {0}", expando.Age); Console.WriteLine("Father: {0}", expando.Family.Father); Console.WriteLine("Mother: {0}", expando.Family.Mother); Console.WriteLine("Brother: {0}", expando.Family.Brother); // change a value expando.Age = 44; // add a new property expando.Family.Sister = "Kathy Smith"; Console.WriteLine("/nModified Values"); Console.WriteLine("Age: {0}", expando.Age); Console.WriteLine("Sister: {0}", expando.Family.Sister); Console.WriteLine("Main method complete. Press Enter."); Console.ReadLine(); } } }
相关文章推荐
- Visual C# 2010 Recipes A Problem-Solution Approach 代码3—— 创建匿名类型
- Visual C# 2010 Recipes A Problem-Solution Approach 代码7——定义转换运算符
- Visual C# 2010 Recipes A Problem-Solution Approach 代码2 —— Create a Windows-Based Application from the Command Line
- Visual C# 2010 Recipes A Problem-Solution Approach 代码一 —— Create a Console Application from the Command Line
- Visual C# 2010 Recipes A Problem-Solution Approach 代码4——自动完成属性
- Visual C# 2010 Recipes A Problem-Solution Approach 代码6——运算符重载
- Visual C# 2010 Recipes: A Problem-Solution Approach
- Description Resource Path Location Type Dynamic Web Module 3.1 requires Java 1.7 or newer. Ascend line 1 Maven Java EE Configuration Problem
- System.Dynamic.ExpandoObject 类型的简单使用
- VS2010使用介绍创建可重用代码(一)---Creating and Using a Dynamic Link Library (C++)
- 在Visual C# 2.0中创建优雅代码3
- 基于Visual Studio2010与C#4.0新功能和展望--dynamic:用DynamicObject创建包装器
- ExpandoObject DynamicObject 的用法
- teamfoundationserver2010之使用vs2010&&vs2013创建修改签入签出提交代码等操作
- 在Visual C# 2.0中创建优雅代码4
- 动态创建菜单,menustrip,根据权限显示菜单,控制菜单可用,反射,给窗体传值,反射对象传值,public static Object CreateInstance ( Type type, pa
- Visual C# 2010新特性之dynamic类型
- 解决"学习python-flask教程,下载源码运行的时候出现:TypeError: 'bool' object is not callable 错误代码:"
- 动态创建菜单,menustrip,根据权限显示菜单,控制菜单可用,反射,给窗体传值,反射对象传值,public static Object CreateInstance ( Type type, params Object[] args )
- 在Visual C# 2.0中创建优雅代码5