views:

26

answers:

0

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
    }
}