Introduced in .NET 4.0, ExpandoObject is a special type designed to be used with dynamic objects. ExpandoObject allows us to add properties, methods, and events to objects at runtime.
To do this, ExpandoObject implements the IDynamicMetaObjectProvider interface, which is basically a dictionary, allowing it to be dynamically extended at runtime. This means you can add properties, methods, and events to an ExpandoObject after the object has been created.
Essentially, ExpandoObject allows for flexibility similar to that of objects in dynamic languages like JavaScript, but in a strongly typed environment like C#.
Creating an ExpandoObject
To work with ExpandoObject, we first need to import the System.Dynamic namespace. Then, you can create an instance of ExpandoObject and dynamically add properties and methods to it.
// Create an instance of ExpandoObject
dynamic person = new ExpandoObject();
// Add properties
person.Name = "Luis";
person.Age = 30;
// Add a method
person.Greet = new Action(() => Console.WriteLine($"Hello, my name is {person.Name} and I am {person.Age} years old."));
// Use the properties and the method
Console.WriteLine($"Name: {person.Name}");
Console.WriteLine($"Age: {person.Age}");
person.Greet();
In this example:
- Instance of
ExpandoObject: A new dynamic objectpersonis created. - Properties: Properties
NameandAgeare added to the object. Due to the dynamic nature ofExpandoObject, you can assign values to them directly. - Method: A method
Greetis added to the object. In this case, the method is defined as an instance ofActionthat prints a personalized greeting. - Usage: The properties are accessed and the method is called as if they were normal members of an object.
Adding and Modifying Properties and Methods Dynamically
The beauty of ExpandoObject lies in its ability to modify its structure at runtime. For example,
// Create an instance of ExpandoObject
dynamic product = new ExpandoObject();
// Add properties
product.Name = "Laptop";
product.Price = 1200;
// Add a method
product.Description = new Func<string>(() => $"The product is a {product.Name} priced at ${product.Price}.");
// Show information
Console.WriteLine(product.Description());
// Modify properties
product.Price = 1100;
Console.WriteLine(product.Description());
In the example,
- Initial Properties and Method: The properties
NameandPriceare initially defined, and a methodDescriptionis added that uses these properties to generate a descriptive string. - Property Modification: After displaying the initial description, the value of the
Priceproperty is updated, and the new description is shown.
Advanced Usage
ExpandoObject can also be used in combination with collections and other dynamic types to create more complex structures.
// Create a list of ExpandoObjects
var products = new List<ExpandoObject>();
// Add products to the list
dynamic product1 = new ExpandoObject();
product1.Name = "Tablet";
product1.Price = 300;
products.Add(product1);
dynamic product2 = new ExpandoObject();
product2.Name = "Smartphone";
product2.Price = 500;
products.Add(product2);
// Show product information
foreach (dynamic product in products)
{
Console.WriteLine($"{product.Name}: ${product.Price}");
}
In this example,
- List of
ExpandoObject: A list containing severalExpandoObjectinstances is created, each representing a product. - Adding Products: Products are added to the list with properties
NameandPrice. - Showing Products: The list is iterated over, and the properties of each product are accessed to display them in the console.