Using DynamicObject (IDynamicMetaObjectProvider) as a component of a static type leads to infinite loop

前端 未结 2 1290
死守一世寂寞
死守一世寂寞 2021-01-31 23:49

I\'m trying to create a dynamic object that can be used as a component of a static object. Here is a contrived example of what I\'m trying to accomplish.

Here is the dy

相关标签:
2条回答
  • 2021-02-01 00:22

    I think the problem is that the Expression parameter passed to GetMetaObject represents the target of the dynamic invocation (i.e. the current object). You are passing the outer object to the call on component.GetMetaObject, so the returned meta object is trying to resolve the call to AMethod on the outer object instead of itself, hence the infinite loop.

    You can create your own meta object which delegates to the inner component when binding member invocations:

    public class AStaticComponent : VendorLibraryClass, IDynamicMetaObjectProvider
    {
        IDynamicMetaObjectProvider component = new DynamicComponent();
    
        public DynamicMetaObject GetMetaObject(Expression parameter)
        {
            return new DelegatingMetaObject(component, parameter, BindingRestrictions.GetTypeRestriction(parameter, this.GetType()), this);
        }
    
        private class DelegatingMetaObject : DynamicMetaObject
        {
            private readonly IDynamicMetaObjectProvider innerProvider;
    
            public DelegatingMetaObject(IDynamicMetaObjectProvider innerProvider, Expression expr, BindingRestrictions restrictions)
                : base(expr, restrictions)
            {
                this.innerProvider = innerProvider;
            }
    
            public DelegatingMetaObject(IDynamicMetaObjectProvider innerProvider, Expression expr, BindingRestrictions restrictions, object value)
                : base(expr, restrictions, value)
            {
                this.innerProvider = innerProvider;
            }
    
            public override DynamicMetaObject BindInvokeMember(InvokeMemberBinder binder, DynamicMetaObject[] args)
            {
                var innerMetaObject = innerProvider.GetMetaObject(Expression.Constant(innerProvider));
                return innerMetaObject.BindInvokeMember(binder, args);
            }
        }
    }
    
    0 讨论(0)
  • 2021-02-01 00:23

    @Lee's answer is really useful, I wouldn't have known where to get started without it. But from using it in production code, I believe it has a subtle bug.

    Dynamic calls are cached at the call site, and Lee's code produces a DynamicMetaObject which effectively states that the inner handling object is a constant. If you have a place in your code where you call a dynamic method on an instance of AStaticObject, and later the same point in the code calls the same method on a different instance of AStaticObject (i.e. because the variable of type AStaticObject now has a different value) then the code will make the wrong call, always calling methods on the handling object from the first instance encountered at that place in the code during that run of the code.

    This is a like-for-like replacement, the key difference being the use of Expression.Field to tell the dynamic call caching system that the handling object depends on the parent object:

    public class AStaticComponent : VendorLibraryClass, IDynamicMetaObjectProvider
    {
        IDynamicMetaObjectProvider component = new DynamicComponent();
    
        public DynamicMetaObject GetMetaObject(Expression parameter)
        {
            return new DelegatingMetaObject(parameter, this, nameof(component));
        }
    
        private class DelegatingMetaObject : DynamicMetaObject
        {
            private readonly DynamicMetaObject innerMetaObject;
    
            public DelegatingMetaObject(Expression expression, object outerObject, string innerFieldName)
                : base(expression, BindingRestrictions.Empty, outerObject)
            {
                FieldInfo innerField = outerObject.GetType().GetField(innerFieldName, BindingFlags.Instance | BindingFlags.NonPublic);
                var innerObject = innerField.GetValue(outerObject);
                var innerDynamicProvider = innerObject as IDynamicMetaObjectProvider;
                innerMetaObject = innerDynamicProvider.GetMetaObject(Expression.Field(Expression.Convert(Expression, LimitType), innerField));
            }
    
            public override DynamicMetaObject BindInvokeMember(InvokeMemberBinder binder, DynamicMetaObject[] args)
            {
                return binder.FallbackInvokeMember(this, args, innerMetaObject.BindInvokeMember(binder, args));
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题