How to make that the LanguageBinder take precedence over the DynamicBinder

Posted by rudimenter on Stack Overflow See other posts from Stack Overflow or by rudimenter
Published on 2010-06-14T22:13:00Z Indexed on 2010/06/14 22:32 UTC
Read the original article Hit count: 284

Hi

I Have a class which implement IDynamicMetaObjectProvider

I implement the BindGetMember Method from DynamicMetaObject.

Now when i Generate a dynamic Object and Access a property every call gets implicit passed through the BindGetMember Method.

I want that at first the language Binder get his chance before my code comes in. It is somehow doable with "binder.FallbackGetMember" but i am not sure how the expression has to look like.

I call here

dynamic com=CommandFactory.GetCommand();

com.testprop; //expected: "test"; but "test2" comes back


 public class Command : System.Dynamic.IDynamicMetaObjectProvider
    {

        public string testprop
        {
            get
            {
                return "test";
            }
        }


        public object GetValue(string name)
        {
            return "test2";
        }


        System.Dynamic.DynamicMetaObject System.Dynamic.IDynamicMetaObjectProvider.GetMetaObject(System.Linq.Expressions.Expression parameter)
        {
            return new MetaCommand(parameter, this);
        }

        private class MetaCommand : System.Dynamic.DynamicMetaObject
        {

            public MetaCommand(Expression expression, Command value)
                : base(expression, System.Dynamic.BindingRestrictions.Empty, value)
            { }

            public override System.Dynamic.DynamicMetaObject BindGetMember(System.Dynamic.GetMemberBinder binder)
        {
            var self = this.Expression;
            var bag = (Command)base.Value;

            Expression target;

            target =
                Expression.Call(
                    Expression.Convert(self, typeof(Command)),
                    typeof(Command).GetMethod("GetValue"),
                    Expression.Constant(binder.Name)
                );


            var restrictions = BindingRestrictions
                                   .GetInstanceRestriction(self, bag);

            return new DynamicMetaObject(target, restrictions);
        }

        #endregion
    }
}

© Stack Overflow or respective owner

Related posts about c#

Related posts about dynamic