Skip to main content

ExpandoObject Class Part-I

ExpandoObject Class


Today I’ll cover the basics of ExpandoObject, which is similar to “dynamic”(covered in last post).

The major dfference in Dynamic and ExpandoObject class is the fact that using an instance of this class, members can be dynamically added and removed at run time.In addition it can be used to get abd set the values of dynamically added members.

The class is a part of System.Dynamic namespace

This class implements the following interfaces.

  • IDynamicMetaObjectProvider
  • IDictionary
  • ICollection>
  • IEnumerable>
  • IEnumerable
  • INotifyPropertyChanged

Example:

dynamic expcheck = new ExpandoObject();

expcheck.Test = " This is Pradeep's Test";

Response.Write(expcheck.Test);

Response.Write(expcheck.Test.GetType());



In the example above, an object of ExpandoObject class is created and and property(“Test”) is added to the object at runtime.

To verify the concept, I print out the values and the output shows the value of the property(“This is Pradeep's Test”) and its type(System.String).


 The expandoObjectClass object can get or set methods,Events of any other functionality that any normal(statically typed) object supports.Th only difference here is whatever the expand object does would be at runtime.

(One of the drawbacks for the developers, in using the expand or dynamic object is no intellisense support in VS-2010.When dynamic or expand is decalred, it is not checked at compile time, rather type checking is done at runtime.Here is should not be misunderstood that there is “No Intellisense support”, in fact there is lot of support.To better understand this, just grab a copy of VS-2010 and experiment).


  
Passing As a Parameter

Instances of the ExpandoObject class can be passed as parameters. These instances are treated as dynamic objects. This means that there is no IntelliSense for object members and there is compiler errors when calling non-existent members. If member that does not exist is called, an exception occurs.

The following code example demonstrates how you can create and use a method to print the names and values of properties.

using System;

using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Dynamic;

namespace WebApplication1

{

public partial class Expando : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
ExpandoCheckMethods();
}
private void ExpandoCheckMethods()
{
dynamic developer, Lead;

developer = new ExpandoObject();
developer.Name = "Pradeep Ptel";
developer.Age = 28;


Lead = new ExpandoObject();
Lead.Name = "Sushil Aggarwal";
Lead.Age = 42;
Lead.TeamSize = 10;

WriteDetails(developer);
// WriteDetails(Lead);

}

private void WriteDetails(dynamic person)
{
// Response.Write(string.Format("{0} is {1} years old.", person.Name, person.Age));// }
Response.Write(string.Format("{0} Manages {1} people", person.Name, person.TeamSize));
}

In the Example above , there would be a runtime exception since the developer object that we are passing as parameter does not have TeamSize Property.

However if we pass “Lead” object the there would be no tuntime exception.

(I recommend, you copy the code and play with it to see how it actually works).


Similar to Dynamic object, I’ll write another post to cover the details of ExpandoObject class soon.

Hope this was Helpful.

Till next time we connect, Happy coding!!

Comments

Popular posts from this blog

Authentication using Social Networking portals(Facebook, Gmail, and Yahoo)

There are tons of sites, which offer sign on using the social networking site credentials (Facebook, gtalk, twitter and the list continues).It can be termed a “SINGLE SIGNON” and offers a lot of benefits compared to traditional database authentication approach. However, not storing user credentials in the DB imposes an additional risk. How to track who all logged into the system. Now the question is, which approach to follow. The best approach is to use inbuilt asp.net users for storing the logging info about user activities and using single signon technique for authentication. This article will explore the approach and provide the details of implementation using some third party libraries and customizing it to the requirements. The Authentication will be done using the following networking portals Yahoo  Gmail Facebook    Special thanks to my friend Sumit Khandelwal, for implementation of Facebook part (in fact he did it all!!) Except Facebook, all other ca...

WCF..Why Communication Foundation?

WCF (Basics..for building effective Services) WCF:  Windows Communication Foundation It includes a collection of of .NET distributed technologies that have existed for long , but never got grouped under one name. WCF can be considered as collection of the following technologies. Web Services(ASMX) NET Enterprise Services MSMQ .NET Remoting Code written in WCF can interact across components, applications and systems. WCF is in accordance with SOA (Service Oriented Architecture). Following Sections provide the details of these ABCs. Addresses In WCF, every service has a unique address. The address provides two important elements A)     Location of the service B)      Transport protocol or transport schema used to communicate with the service.  The location indicates the name of the target machine, site, or network; a communication port, pipe, or queue; and an optional specific path or URI. WCF supports th...

Interview Queations-A Quick Recap

Today one of my friends, got his first blog entry published. I find it cool enough to get started and it can come up as an excellent reource for quick review of concepts prior to interview. Here is the link. Quick Review Hope this is helpful! Till next we Connect........ Happy Learning.