mercredi 8 novembre 2017

How to access properties in an object derived from DynamicObject

Days ago i asked this question and my problem was solved for that case. The problem now is that i can't access properties like i was able to do with a normal dynamic, let me explain with some code:

dynamic d = new ExpandoObject();
d.SomeProp = "foo"; //works

CustomDynamic c = new CustomDynamic();
c.SomeProp = "bar"; //Not works 

Here is my implementation of CustomDynamic

public class CustomDynamic : DynamicObject
{
    private readonly Dictionary<string, object> _dictionary = new Dictionary<string, object>();

    public override bool TryGetMember(GetMemberBinder binder, out object result)
    {
        var name = binder.Name.ToLower();
        result = _dictionary.ContainsKey(name) ? _dictionary[name] : null;
        return true;
    }

    public override bool TrySetMember(SetMemberBinder binder, object value)
    {
        _dictionary[binder.Name.ToLower()] = value;
        return true;
    }
}

What can i implement to make my CustomDynamic behaves like a normal dynamic





Aucun commentaire:

Enregistrer un commentaire