ExpandoObject - SkillBakery Studios

Breaking

Post Top Ad

Post Top Ad

Monday, September 7, 2020

ExpandoObject

Expando is expandable objects which means you can add properties, methods, and events at runtime. You can also set and get the values of such members

ExpandoObject is just a dictionary which holds object values based on string keys, that means iterating over ExpandoObject is as easy as iterating through a regular dictionary.

ExpandoObject type allows you to define objects on the fly and then add properties to it, it inherits the same advantages and disadvantages.

It has several interacting interfaces. One of them is IDictionary<string, object>

Let’s understand with a little code

We use the dynamic keyword to hold an object where we define properties without having to define the class first. If you need to use C# comes with a solution for you

The ExpandoObject

      dynamic   user = new System.Dynamic.ExpandoObject();

      user.name =”Bhawna Tiwari”;

       user.Age  =  35;

       Console.writeline(user.name + “is” + user.Age)

 You will notice that I declare the object with the dynamic type, even though I instantiate an ExpandoObject.The reason is that if the object was declared as an ExpandoObject compiler would check.

 Creating a dynamic, extensible C# ExpandoObject

 ExpandoObject in .NET 4.0

.NET 4.0 framework actually includes an ExpandoObject class which provides a dynamic object that allows you to add properties & methods on the fly and then access them again.

 

For e.g with ExpandoObject, you can do stuff

 dynamic expand = new ExpandoObject();

expand.Name = “Bhawna”;

expand.HelloWorld =(Func<string,string>)

(IString  name)=>

{

Return “Hello” + name ;

});

Console.Writeline(expand.Name);

Console.Writeline(expand.HelloWorld(“Tiwari”);

 Internally ExpandoObject uses a dictionary-like structure and interface to store properties & methods and allows you to add and access properties & methods easily.

Working with dynamic objects

if we need a dynamic container for a dictionary-like structure, ExpandoObject does not work because it’s a scaled the class that cant be inherited.

 

Public class user: Westwind.Utilities.Dynamic.Expando

{

Public string Name {get; set;}

Public string Email {get;set;}

Public string Password{get; set;}

Public string DateTime{get; set;}

}

And then we start Expanding the properties of this object dynamically?

 

Public void UserExample()

{

Var user =new user();

//set strongly typed properties

User.Name =”Bhawna”;

User.Email= “tiwaribhawna29mar”;

User.Password=”tiwari123”;

User.Active =” true”;

 

//Adding dynamic properties

dynamic duser =user;

duser.entered= DateTime.Now;

duser.Acesses=1;

//dynamic properties via indexer

user[“NickName”]= “TechieBhawna”;

duser[“website”]=http://www.skillbakery.com/blog;

 

 //loop through all properties dynamic &strong type

foreach(var prop in user.GetProperties(true))

{

Object val = prop.value;

If(val==null)

Val=”null”;

Console.writeline(prop.key +”;” +val.Tostring());

}

}

 

In the above code, you can start with strongly typed object that has a fixed set of properties.

To access the strongly typed properties you can use either strongly typed instance, the indexer, or the dynamic cast of the object.

To access the dynamically added properties use either the indexer on the strongly typed object property syntax on the dynamic cast.

 Working with dynamic properties :

A scenario where you’re reading a CSV file and parsing its data into a set of ExpandoObjects.You’re using ExpandoObjects because each CSV file has a different set of columns & you want the property names on your ExpandoObject to reflect those column names.

With ExpandoObjects you can use same syntax as you would with a dictionary to store your values.

Cast your ExpandoObject to the IDictionary interface and use your ExpandoObject like a Dictionary.

Here’s a method that accepts both a property name and property value and adds them both to ExpandoObject

 public  ExpandoObject

createDynamicUser (string  PropertyName, string  PropertyValue)

{

 dynamic user= new ExpandoObject();

((IDictionary < string , object>) user)

[propertyName] = propertyValue;

 return user;

}

So, I added the property through IDictionary interface, and can retrieve it just like an ordinary property

 dynamic  user = CreateDynamicUser(“FullName”, “Bhawna Tiwari”);

 string fname =user.FullName;

Adding properties dynamically in one part of your application. The solution here  again is to cast your ExpandoObject as an IDictionary object.

Foreach(keyValuePair<string ,object>

Kvp in ((IDictionary <string, object>)

User))

{

String PropertyWithValue =kvp.key + “;” + KVP.value.Tostring();

}

If you’re writing code like this, then you probably don’t want to work. To filter out any members loaded with an Action expression.

Foreach(KeyValuePair Cstring, object)

Kvp in

((IDictionary <string, object>)

User))

{

If(!kvp.value.GatType().Name.Contain)

(“Action”))

{

Foreach(KeyValuePair <string,object>

Kvp in

((IDictionary <string,object>)

User))

{

String.PropertyWithValue= kvp.key +”;” + kvp.value.Tostring();

}

Your ExpandoObject to the IDictionary interface, you’re free to use other methods built into IDictionary interface, including contains key.

 

Storing ExpandoObjects to capture information

You are going to store that information somewhere that means you’ ill need to convert your ExpandoObject into some “Storable” format

String struser =JsonConvert.SerializeObject(user, newExpandoObjectConverter());

 

No comments:

Post a Comment

Post Top Ad