csharp-tipo-expando

The `ExpandoObject` Type in C#

  • 3 min

Introduced in .NET 4.0, ExpandoObject is a special type designed for use with dynamic objects. ExpandoObject allows us to add properties, methods, and events to objects at runtime.

To achieve this, ExpandoObject implements the IDynamicMetaObjectProvider interface, which is essentially a dictionary, enabling 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 objects in dynamic languages like JavaScript, but within 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();
Copied!

In this example:

  • Instance of ExpandoObject: A new dynamic object person is created.
  • Properties: Properties Name and Age are added to the object. Due to the dynamic nature of ExpandoObject, you can assign values to them directly.
  • Method: A Greet method is added to the object. In this case, the method is defined as an instance of Action that 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());
Copied!

In the example,

  1. Initial Properties and Method: The properties Name and Price are initially defined, and a Description method is added that uses these properties to generate a descriptive string.
  2. Modifying Properties: After displaying 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}");
}
Copied!

In this example,

  • List of ExpandoObject: A list is created containing several ExpandoObject instances, each representing a product.
  • Adding Products: Products are added to the list with Name and Price properties.
  • Displaying Products: The list is iterated through, and the properties of each product are accessed to display them in the console.