Dynamically Adding Properties to Existing Objects in C#
If you are not a Medium member, Click here to read the full article.
Dynamically adding properties to existing objects in C# is not as easy as in Python or JavaScript because C# is a strongly typed language.
However, we can achieve this by using some techniques and libraries such as extension methods, dictionaries, ExpandoObject, etc. This article will detail how to implement this in C#.

Method 1: Using ExpandoObject
ExpandoObject is a special class provided by .NET that allows dynamic addition of properties. It implements the IDictionary<string, object> interface, meaning you can dynamically add properties as you would with a dictionary.
using System;
using System.Dynamic;
namespace DynamicPropertiesExample
{
class Program
{
static void Main(string[] args)
{
dynamic expando = new ExpandoObject();
expando.Name = "John Doe";
expando.Age = 30;
// Add new properties
expando.Country = "USA";
expando.Occupation = "Engineer";
// Print output
Console.WriteLine($"Name: {expando.Name}");
Console.WriteLine($"Age: {expando.Age}");
Console.WriteLine($"Country: {expando.Country}");
Console.WriteLine($"Occupation: {expando.Occupation}");
// Iterate through all properties
foreach (var prop in (IDictionary<string, object>)expando)
{
Console.WriteLine($"{prop.Key}: {prop.Value}");
}
}
}
}Method 2: Using Anonymous Types
Anonymous types can also be used to dynamically add properties, although they are usually used in static scenarios. This is another method.
using System;
namespace DynamicPropertiesExample
{
class Program
{
static void Main(string[] args)
{
var person = new { Name = "Jane Doe", Age = 25 };
// Create another object using anonymous type to add new properties
var extendedPerson = new
{
person.Name,
person.Age,
Country = "Canada",
Occupation = "Designer"
};
// Print output
Console.WriteLine($"Name: {extendedPerson.Name}");
Console.WriteLine($"Age: {extendedPerson.Age}");
Console.WriteLine($"Country: {extendedPerson.Country}");
Console.WriteLine($"Occupation: {extendedPerson.Occupation}");
}
}
}Method 3: Using Extension Methods
While extension methods cannot directly add properties, they can extend the functionality of existing types. Similarly, we can achieve a similar effect by combining dictionaries.
using System;
using System.Collections.Generic;
namespace DynamicPropertiesExample
{
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
public static class PersonExtensions
{
private static readonly Dictionary<Person, Dictionary<string, object>> _additionalProperties = new();
public static void AddProperty(this Person person, string propertyName, object value)
{
if (!_additionalProperties.ContainsKey(person))
{
_additionalProperties[person] = new Dictionary<string, object>();
}
_additionalProperties[person][propertyName] = value;
}
public static object GetProperty(this Person person, string propertyName)
{
if (_additionalProperties.ContainsKey(person) && _additionalProperties[person].ContainsKey(propertyName))
{
return _additionalProperties[person][propertyName];
}
throw new KeyNotFoundException($"Property '{propertyName}' not found.");
}
}
class Program
{
static void Main(string[] args)
{
var person = new Person { Name = "Alice", Age = 28 };
// Add dynamic properties
person.AddProperty("Country", "UK");
person.AddProperty("Occupation", "Teacher");
// Get and print properties
Console.WriteLine($"Name: {person.Name}");
Console.WriteLine($"Age: {person.Age}");
Console.WriteLine($"Country: {person.GetProperty("Country")}");
Console.WriteLine($"Occupation: {person.GetProperty("Occupation")}");
}
}
}Through these methods, we can dynamically add multiple properties to existing objects in C#. Although C# is a strongly typed language, we can still implement dynamic features like reflection and object extension through these techniques. Choosing the appropriate method based on specific needs can effectively improve the flexibility and extensibility of your code.





