Dynamic objects in C# are objects that can have properties and methods added to them at runtime, as opposed to traditional objects which have a fixed set of properties and methods defined at compile time. There are a few different ways to create dynamic objects in C#, including the dynamic keyword and the ExpandoObject class.
Here’s and example of using the dynamic keyword to create a dynamic object:
dynamic obj = new ExpandoObject();
obj.Name = "John Smith";
obj.Age = 30;
console.WriteLine($"Name: {obj.Name}, Age: {obj.Age}");
In the example above, we create a dynamic object using the ExpandoObject class, which is part of the System.Dynamic namespace. We then add a Name and Age property to the object, and print them out.
Here’s an example of using the ExpandoObject class to create a more powerful and flexible dynamic object:
dynamic obj = new MyDynamicObject();
obj.Name = "John Smith";
obj.Age = 30;
console.WriteLine($"Name: {obj.Name}, Age: {obj.Age}");
public class MyDynamicObject : DynamicObject
{
private Dictionary<string, object> _properties = new Dictionary<string, object>();
public override bool TrySetMember(SetMemberBinder binder, object value)
{
_properties[binder.Name] = value;
return true;
}
public override bool TryGetMember(GetMemberBinder binder, out object result)
{
return _properties.TryGetValue(binder.Name, out result);
}
}
In the example above, we define a MyDynamicObject class that inherits from DynamicObject, and overrides the TrySetMember and TryGetMember methods. These methods allow us to add and retrieve properties from the object at runtime.
Dynamic objects can be useful in a variety of situations, such as when you need to work with data that has a flexible schema, or when you want to add properties and methods to an object at runtime based on user input. However, it’s important to keep in mind that using dynamic objects can make your code less predictable and more difficult to debug, so it’s usually best to use them sparingly. The ExpandoObject is a more powerful and flexible option than the dynamic keyword, but it may be more complex to use in some cases.
dynamic vs expando
dynamicis a type, whereasExpandoObjectis a class: Thedynamickeyword is a type that can be used to declare a variable. When you declare a variable asdynamic, you can assign any value to it at runtime. TheExpandoObjectclass, on the other hand, is a class that you can use to create a dynamic objectdynamicobjects are resolved at runtime, whereasExpandoObjectobjects are resolved at runtime and compile time: When you use thedynamickeyword, the compiler does not check the type of the object at compile time. Instead, the type is resolved at runtime, which can make it more difficult to catch errors. TheExpandoObject, on the other hand, is resolved at both runtime and compile time, which means that the compiler can catch some errors that might occur when using the object.dynamicobjects have limited functionality, whereasExpandoObjectobjects have more functionality: Thedynamickeyword is limited to the functionality provided by the .NET runtime. This means that you cannot add your own methods or properties to adynamicobject. TheExpandoObject, on the other hand, is part of theSystem.Dynamicnamespace, which provides a number of classes and methods for working with dynamic objects. This means that you can use theExpandoObjectto create more powerful and flexible dynamic objects.
Conclusion
In general, the ExpandoObject is a more powerful and flexible option than the dynamic keyword, since it provides more functionality and is resolved at both runtime and compile time. However, the dynamic keyword can be a simpler and more lightweight option in some cases, particularly when you don’t need the additional functionality provided by the ExpandoObject.


Leave a comment