Introduced in .NET 4.0, ExpandoObject
is a special type designed to work with dynamic objects. The 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.
In essence, ExpandoObject
provides 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 method
Console.WriteLine($"Name: {person.Name}");
Console.WriteLine($"Age: {person.Age}");
person.Greet();
In this example:
- Instance of
ExpandoObject
: A new dynamic objectperson
is created. - Properties: Properties
Name
andAge
are added to the object. Due to the dynamic nature ofExpandoObject
, you can assign values to them directly. - Method: A method
Greet
is added to the object. In this case, the method is defined as an instance ofAction
that prints a personalized greeting. - Usage: The properties are accessed, and the method is called as if they were normal members of an object.
Dynamically Adding and Modifying Properties and Methods
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
Name
andPrice
are initially defined, and a methodDescription
is added that uses these properties to generate a descriptive string. - Modification of Properties: After showing the initial description, the value of the
Price
property is updated and the new description is displayed.
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 is created that contains severalExpandoObject
, each representing a product. - Adding Products: Products are added to the list with properties
Name
andPrice
. - Displaying Products: The list is iterated over, and the properties of each product are accessed to display them on the console.