s

C# .net add property to expando object dynamically edit button Edit

author
Murugan Andezuthu Dharmaratnam | calendar 12 January 2022 | 1624

sample code to add property to a C# expando object dynamically

Sample Code

        public void AddProperty(ExpandoObject expando, string propertyName, object propertyValue)
        {
            // ExpandoObject supports IDictionary so we can extend it like this
            var expandoDict = expando as IDictionary<string, object>;
            if (expandoDict.ContainsKey(propertyName))
                expandoDict[propertyName] = propertyValue;
            else
                expandoDict.Add(propertyName, propertyValue);
        }