您的位置:首页 > 移动开发 > Objective-C

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();
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐