Skip to content

Instantly share code, notes, and snippets.

@ddjerqq
Created May 13, 2025 11:47
Show Gist options
  • Save ddjerqq/deb6d7d62782014e2e02ffe779727ed1 to your computer and use it in GitHub Desktop.
Save ddjerqq/deb6d7d62782014e2e02ffe779727ed1 to your computer and use it in GitHub Desktop.
public static class ExpandoExt
{
/// <summary>
/// Add a property to a dynamic object at runtime.
/// </summary>
/// <example>
/// dynamic expando = new ExpandoObject();
/// expando.Name = "Brian";
/// expando.Country = "USA";
/// expando.AddProperty("Language", "English");
/// </example>
/// <param name="expando">The dynamic object to which the property will be added.</param>
/// <param name="key">The name of the property to add.</param>
/// <param name="value">The value of the property to add.</param>
public static void AddProperty(this System.Dynamic.ExpandoObject expando, string key, object value) =>
(expando as IDictionary<string, object>)[key] = value;
}
@ddjerqq
Copy link
Author

ddjerqq commented May 13, 2025

Usage

dynamic expando = new ExpandoObject();
expando.Name = "Brian";
expando.Country = "USA";
expando.AddProperty("Language", "English");

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment