dev hedgehog
dev hedgehog

Reputation: 8801

Dynamically add new property with any name to object

Well I guess the title explains it all guys.

I am fairly new to dynamics world in c# and I am seeking for a way to do something like this:

public dynamic AddPropertyToObject(Foo f, string varPropName)
{
    f.Str = "Hello";
    dynamic dynObj = f;
    dynObj.createPropertyNameAndSetValue(varPropName, "World");
    return dynObj;
}

The problem is that an object shall be extended by a new property with any given name.

The object (in my example its Foo) is an entity generated from wsdl and therefore it doesnt inherit from ExpandoObject.

How could I still achieve the object to be extended?

createPropertyNameAndSetValue is just there to visualize that I am seeking for something like this: dynObject.varPropName = "World";

Btw, I am looking for a solution without PropertyInfo and all that stuff.

I am sorry if this question is a duplicate. Let me know in comments before downvoting and I'll remove it.

Upvotes: 0

Views: 1586

Answers (2)

Saravanan
Saravanan

Reputation: 930

Workaround using composition. Taking "Foo" itself as a property of dynamic object in the extension method

public static object AddProperty<T>(this Object baseObject,string baseObjectPropertyName, string propertyName, T value)
{
    IDictionary<string, object> ob;
    if (baseObject is IDictionary<string, object>)
    {
        ob = baseObject as IDictionary<string, Object>;
    }
    else
    {
        ob = new ExpandoObject() as IDictionary<string, Object>;
        ob.Add(baseObjectPropertyName, baseObject);
    }
    ob.Add(propertyName, value);
    return ob;
}


var foo = new Foo();
var goingTobeIgnored = string.Empty;
dynamic ob = foo.AddProperty("FooObject","Hello", "World")
            .AddProperty(goingTobeIgnored, "AnotherHello", "AnotherWorld");

Upvotes: 0

Joe White
Joe White

Reputation: 97818

You can't add properties to just anything. You need a class that knows how to integrate with the dynamic type system, and that has someplace to store the extra properties that you add to it.

Take a look at ExpandoObject. Create an instance of it and then start setting properties on it.

From your comments, it sounds like you want to extend an existing type to support something similar. The documentation for ExpandoObject will still help you out; it talks about how it's implemented, so it'll help you implement it on your own. The Remarks section explains:

If you want to define types that have their own dynamic dispatch semantics, use the DynamicObject class. If you want to define how dynamic objects participate in the interoperability protocol and manage DLR fast dynamic dispatch caching, create your own implementation of the IDynamicMetaObjectProvider interface.

So you'll either need to change your class to descend from DynamicObject (which implements IDynamicMetaObjectProvider and does some of the grunt work for you), or you'll need to implement IDynamicMetaObjectProvider yourself.

Keep reading the docs, and you'll find a link to an article called Implementing Dynamic Interfaces, which should help you decide which is better in your case (descending, or implementing the interface directly).

Upvotes: 3

Related Questions