Creates an ExpandoObject and writes out the property values: - CSharp Custom Type

CSharp examples for Custom Type:dynamic type

Description

Creates an ExpandoObject and writes out the property values:

Demo Code

using System;/*w  ww  .  j av  a 2s . c  o  m*/
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Dynamic;
class MainClass
{
   static void Main(string[] args)
   {
      // create the expando
      dynamic expando = new ExpandoObject();
      expando.Name = "Mary";
      expando.Age = 42;
      expando.Family = new ExpandoObject();
      expando.Family.Father = "f";
      expando.Family.Mother = "m";
      expando.Family.Brother = "b";
      // 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);
   }
}

Result


Related Tutorials