ExpandoObject class in C# 4.0

As you know till now in c# everything was static and known at design time. But with the C# 4.0  language Microsoft have enabled new dynamic data type which is belongs to Dynamic Language Runtime on top of CLR(Common language Runtime). As you know that dynamic object will know their behaviour at run time. Here Microsoft has given one new class called ExpandoObject class. ExpandoObject class is a member of System.Dynamic namespace and is defined in the System.Core assembly. This class object members can be dynamically added and removed at runtime. This is class is a sealed class and implements number of interfaces like below.

public sealed class ExpandoObject : 
  IDynamicMetaObjectProvider, 
  IDictionary<string, object>, 
  ICollection<KeyValuePair<string, object>>, 
  IEnumerable<KeyValuePair<string, object>>, 
  IEnumerable, 
  INotifyPropertyChanged;

Now let’s take a simple example of console application. Where we will create a object of expandoobject class and manipulate that class at run time. Let’s create a simple code like below.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ExpandoObject
{
    class Program
    {
        static void Main(string[] args)
        {
            dynamic users = new System.Dynamic.ExpandoObject();
            users.UserName = "Jalpesh";
            users.Password = "Password";

            Console.WriteLine(string.Format("{0}:{1}","UserName:",users.UserName));
            Console.WriteLine(string.Format("{0}:{1}","Password:",users.Password));

            Console.ReadKey();
        }
    }
}

Here in the above code I have added a new memeber called UserName and Password for the new dynamic user type and then print their value. Now let’s run the application and see its output as below

ExpandoObject

That’s it. You can add valid type of member to ExpandoOjbect class. Isn’t interesting.. Hope you liked it.. Stay tuned for more..

Shout it
Published Thursday, April 14, 2011 2:56 AM by Jalpesh P. Vadgama

Comments

# Object Oriented Programing &raquo; ExpandoObject class in C# 4.0

Pingback from  Object Oriented Programing &raquo; ExpandoObject class in C# 4.0

# Do-All Outdoors White Wing Automatic Trap Reviews

Thursday, April 14, 2011 12:53 AM by Do-All Outdoors White Wing Automatic Trap Reviews

Pingback from  Do-All Outdoors White Wing Automatic Trap Reviews

# News about Tarp Law and Foreclosures issue #1

Thursday, April 14, 2011 1:59 AM by News about Tarp Law and Foreclosures issue #1

Pingback from  News about Tarp Law and Foreclosures issue #1

# re: ExpandoObject class in C# 4.0

Thursday, April 14, 2011 2:21 AM by Pjotr

Hello Jalpesh P. Vadgama,

What is your intention of this article? The same info is spread all over the Internet.

# Google class action privacy suit finally settled | San Francisco DUI Attorney

Pingback from  Google class action privacy suit finally settled | San Francisco DUI Attorney

# re: ExpandoObject class in C# 4.0

Thursday, April 14, 2011 3:56 AM by Jalpesh P. Vadgama

@Pjotr- This blog is for my readers and peoples who don't know this stuff. I am writing blog for my readers.

There are hundreds of people who don't know this kind of stuff and they are not doing smart programming and I want to give them a new way of doing programming.

Do you write blogs? If you are not writing then you should write blog and then you will understand what kind of stuff you need to put on your blog. If you don't be positive about this then please don't be negative about.

Hope you take this in positive manners.

Best Regards,

Jalpesh

# Premature Ejaculation Treatment ??? Find the easiest way to deal &#8230; | Premature Ejaculation Guide

Pingback from  Premature Ejaculation Treatment ??? Find the easiest way to deal &#8230; | Premature Ejaculation Guide

# TF2 Support Characters On Game Servers

Thursday, April 14, 2011 4:12 AM by TF2 Support Characters On Game Servers

Pingback from  TF2 Support Characters On Game Servers

# SocialMirchi.com &raquo; The rich Indian cuisines are getting popular in abroad????????????via internet

Pingback from  SocialMirchi.com   &raquo; The rich Indian cuisines are getting popular in abroad????????????via internet

# ExerBeat ??? New Screenshots and Information | Wii Word Gossip

Pingback from  ExerBeat ??? New Screenshots and Information | Wii Word Gossip

# Your Questions About Home Alarm Security Stickers | AlarmGenie Home Alarm Security

Pingback from  Your Questions About Home Alarm Security Stickers | AlarmGenie Home Alarm Security

# Dog Obedience Training Tips

Thursday, April 14, 2011 12:10 PM by Dog Obedience Training Tips

Pingback from  Dog Obedience Training Tips

# News about Foreclosure Class Action Lawsuits issue #1

Thursday, April 14, 2011 1:49 PM by News about Foreclosure Class Action Lawsuits issue #1

Pingback from  News about Foreclosure Class Action Lawsuits issue #1

# Leather Organizer Purse |

Thursday, April 14, 2011 2:27 PM by Leather Organizer Purse |

Pingback from  Leather Organizer Purse |

# Sarasota Luxury Real-estate Offers Beauty And Magnificence | reservationsfor2.com

Pingback from  Sarasota Luxury Real-estate Offers Beauty And Magnificence | reservationsfor2.com

# Posts about 870HELP issue #1

Tuesday, April 19, 2011 2:29 AM by Posts about 870HELP issue #1

Pingback from  Posts about 870HELP issue #1

# News about Public Notices Foreclosure Sale issue #1

Wednesday, April 20, 2011 5:40 PM by News about Public Notices Foreclosure Sale issue #1

Pingback from  News about Public Notices Foreclosure Sale issue #1

# News about Homestead Law and Foreclosure issue #1

Thursday, April 21, 2011 11:02 PM by News about Homestead Law and Foreclosure issue #1

Pingback from  News about Homestead Law and Foreclosure issue #1

# Posts about 870HELP issue #1

Friday, April 22, 2011 11:33 PM by Posts about 870HELP issue #1

Pingback from  Posts about 870HELP issue #1

# How To Deal With Students Learning Challenges | ArticleVIP

Wednesday, December 21, 2011 12:48 PM by How To Deal With Students Learning Challenges | ArticleVIP

Pingback from  How To Deal With Students Learning Challenges | ArticleVIP