Language: EN

csharp-tipo-expando

The `ExpandoObject` Type in C#

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 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 method Greet 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());

In the example,

  1. Initial Properties and Method: The properties Name and Price are initially defined, and a method Description is added that uses these properties to generate a descriptive string.
  2. Property Modification: After displaying the initial description, the value of the Price property 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 several ExpandoObject instances is created, each representing a product.
  • Adding Products: Products are added to the list with properties Name and Price.
  • Showing Products: The list is iterated over, and the properties of each product are accessed to display them in the console.