Wednesday, 2 March 2011

Infinite Loop in DLR caching with DynamicObject

This is another one of those posts to give the answer to a problem that I couldn’t find with Google.

I was implementing a method call by:

  • a BindGetMember on a dynamic meta object, returning an expression which returned a new dynamic meta object provider for the method.
  • a BindInvoke on the dynamic meta object for the method.

Because the dynamic meta object provider for the method didn’t pass itself (i.e. this) into the value parameter for the dynamic meta object representing the method, the binding restrictions created didn’t match the expected type and so they weren’t cached by the CLR.

Instead, it just looped round and round (in System.Dynamic.UpdateDelegates.UpdateAndExecute2) trying to cache the new rule. The fix was to make sure that the DynamicMetaObject has a Value of the original IDynamicMetaObjectProvider instance.

Hope that helps someone else.