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

Asp.Net 4.0: An Overview-Part-III

This is the last post in the series which will explore the following new features of ASP.Net 4.0  Performance Monitoring for Individual Applications in a Single Worker Process Web.config File Refactoring Permanently Redirecting a Page Expanding the Range of Allowable URLs Performance Monitoring for Individual Applications in a Single Worker Process It is a common practice to host multiple ASP.NET applications in a single worker process, In order to increase the number of Web sites that can be hosted on a single server. This practice results in difficulties for server administrators to identify an individual application that is experiencing problems. ASP.NET 4 introduces new resource-monitoring functionality introduced by the CLR. To enable this functionality, following XML configuration snippet is added to the aspnet.config configuration file.(This file is located in the directory where the .NET Framework is installed ) <?xml version="1.0" encoding="UTF-8...

Covariance and Contravariance-General Discussion

If you have just started the exploration of .Net Framework 4.0, two terms namely Covariance and Contravariance might have been heard. The concept that these terms encapsulate are used by most developer almost daily, however there has never been any botheration about the terminologies. Now, what actually these terms mean and how are these going to affect us as a developer, if we dive in to the details. The simple answer is it’s always good to know your tools before actually using them. Enough philosophy, let’s get to the business. Starting the discussion let me reiterate that in addition to Covariance and Contravariance, there is another terminology, Invariance. I’ll by start here by diving into the details of Invariance and then proceed further. Invariance: Invariance can be better understood by considering the types in .Net.>net has basically two type, value-types and reference-types. Value types (int, double etc) are invariant i.e. the types can’t be interchanged either ...

Advanced WCF

In this post, I am sharing the link of articles about  advanced topics in WCF. The List of articles is exhaustive and can serve as your repository for all WCF queries. Concurrency,Throttling & Callbacks  WCF Concurrency (Single, Multiple and Re entrant) and Throttling   WCF-Interop and BinarySecurityToken  WCF Callbacks  Creating Web Services From WSDL Link1 Link2 Link3 Link4 WCF-Security WCF over HTTPS   Transport Security(basic)/HTTPS UserNamePasswordValidator ServerCertificateValidationCallback 9 simple steps to enable X.509 certificates on WCF - CodeProject http://www.codeproject.com/KB/WCF/9StepsWCF.aspx?display=Print Message Security(Certificate)/PeerTrust Securing WCF Services with Certificates. - CodeProject http://www.codeproject.com/KB/WCF/wcf_certificates.aspx Message Security(Certificate)/ChainTrust How To Configure WCF Security Using Only X.509 Certificates - CodePr...