lundi 19 août 2019

Dynamically assigning properties to ExpandoObject

I need to dynamically add properties with values to an anonymous object so I am using the ExpandoObject with a dynamic variable. However, I don't know the property names before runtime and I don't want to hard code many properties using routeValues.MyProp = "value" for different scenarios. It turns out the ExpandoObject doesn't like using indexers of the form routeValues[prop.Name] = value;as it causes this error:

enter image description here

This is the method:

public object GetRouteValues() {
    var properties = GetType().BaseType.GetProperties();
    dynamic routeValues = new ExpandoObject();

    foreach (var prop in properties)
    {
        var value = prop.GetValue(this) as string;

        if (!string.IsNullOrWhiteSpace(value))
            routeValues[prop.Name] = value; // causes error
    }

    return routeValues;
}

How can I dynamically add properties whose name I don't know until runtime to a dynamic object? Thanks.





Aucun commentaire:

Enregistrer un commentaire