0

My DynamicObject implementation looks like this:

public class DynCallsite: DynamicObject
{
    public DynCallsite(ScriptPlayer player)
    {
        _player = player;
    }

    public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result)
    {
        var ftt = new CallTranslator();

        var request = ftt.CreateXmlRequest(binder.Name, args);

        var callResult = _player.CallFunction(request);
        result = ftt.DeserializeXmlRequest(callResult);

        return true;            
    }

    private ScriptPlayer _player;
}

This is how I'm using the instance of this object:

class DynHost
{
    public DynHost()
    {
        _callSite = new DynCallsite(new ScriptPlayer());
    }    

    public dynamic Callsite
    {
        get { return _callsite; }   // A breakpoint put here will be hit
    }
}

// *snip*

var dh = new DynHost();
dh.Callsite.MyMethod("str1", 5, "str2");

It used to work :), I'm not sure what changed. The debugger is not breaking on the TryInvokeMember call and I'm getting a RuntimeBinderException. This class is defined in a different assembly than the running one (it is referenced the normal way, by adding references to the project from the same solution).

After performing a call on an instance, I'm getting the following stack trace:

Microsoft.CSharp.RuntimeBinder.RuntimeBinderController.SubmitError(Microsoft.CSharp.RuntimeBinder.Errors.CError pError) + 0x23 bytes
Microsoft.CSharp.RuntimeBinder.Semantics.LangCompiler.SubmitError(Microsoft.CSharp.RuntimeBinder.Errors.CParameterizedError error) + 0x24 bytes
Microsoft.CSharp.RuntimeBinder.Errors.ErrorHandling.ErrorTreeArgs(Microsoft.CSharp.RuntimeBinder.Errors.ErrorCode id, Microsoft.CSharp.RuntimeBinder.Errors.ErrArg[] prgarg) + 0x53 bytes Microsoft.CSharp.RuntimeBinder.Semantics.MemberLookup.ReportErrors() + 0x6cd bytes
Microsoft.CSharp.RuntimeBinder.RuntimeBinder.BindCall(Microsoft.CSharp.RuntimeBinder.ICSharpInvokeOrInvokeMemberBinder payload, Microsoft.CSharp.RuntimeBinder.Semantics.EXPR callingObject, Microsoft.CSharp.RuntimeBinder.RuntimeBinder.ArgumentObject[] arguments, System.Collections.Generic.Dictionary dictionary) + 0x206 bytes Microsoft.CSharp.RuntimeBinder.RuntimeBinder.DispatchPayload(System.Dynamic.DynamicMetaObjectBinder payload, Microsoft.CSharp.RuntimeBinder.RuntimeBinder.ArgumentObject[] arguments, System.Collections.Generic.Dictionary dictionary) + 0xb1 bytes
Microsoft.CSharp.RuntimeBinder.RuntimeBinder.BindCore(System.Dynamic.DynamicMetaObjectBinder payload, System.Collections.Generic.IEnumerable parameters, System.Dynamic.DynamicMetaObject[] args, out System.Dynamic.DynamicMetaObject deferredBinding) + 0xbc bytes
Microsoft.CSharp.RuntimeBinder.RuntimeBinder.Bind(System.Dynamic.DynamicMetaObjectBinder payload, System.Collections.Generic.IEnumerable parameters, System.Dynamic.DynamicMetaObject[] args, out System.Dynamic.DynamicMetaObject deferredBinding) + 0x56 bytes
Microsoft.CSharp.RuntimeBinder.BinderHelper.Bind(System.Dynamic.DynamicMetaObjectBinder action, Microsoft.CSharp.RuntimeBinder.RuntimeBinder binder, System.Collections.Generic.IEnumerable args, System.Collections.Generic.IEnumerable arginfos, System.Dynamic.DynamicMetaObject onBindingError) + 0x2ca bytes Microsoft.CSharp.RuntimeBinder.CSharpInvokeMemberBinder.FallbackInvokeMember(System.Dynamic.DynamicMetaObject target, System.Dynamic.DynamicMetaObject[] args, System.Dynamic.DynamicMetaObject errorSuggestion) + 0x77 bytes
System.Dynamic.DynamicObject.MetaDynamic.BindInvokeMember.AnonymousMethod__10(System.Dynamic.DynamicMetaObject e) + 0x1b bytes
System.Dynamic.DynamicObject.MetaDynamic.BindInvokeMember(System.Dynamic.InvokeMemberBinder binder, System.Dynamic.DynamicMetaObject[] args) + 0xb8 bytes System.Dynamic.InvokeMemberBinder.Bind(System.Dynamic.DynamicMetaObject target, System.Dynamic.DynamicMetaObject[] args) + 0x36 bytes System.Dynamic.DynamicMetaObjectBinder.Bind(object[] args, System.Collections.ObjectModel.ReadOnlyCollection parameters, System.Linq.Expressions.LabelTarget returnLabel) + 0xea bytes System.Runtime.CompilerServices.CallSiteBinder.BindCore>(System.Runtime.CompilerServices.CallSite> site, object[] args) + 0x80 bytes System.Dynamic.UpdateDelegates.UpdateAndExecuteVoid3(System.Runtime.CompilerServices.CallSite site, object arg0, decimal arg1, double arg2) + 0x30e bytes MyApp.DynCallsite.MyMethod(string str1, decimal number, string str2) Line 96 + 0x17d bytes C#

It pretty much looks like as if I was using a DynamicObject instance instead of my specialized derived class. However, I confirmed that this is not the case, as I can inspect the callsite object in the debugger and it says DynCallsite. What could cause this?

Tamás Szelei
  • 23,169
  • 18
  • 105
  • 180
  • 1
    You haven't shown how you're *using* the object. If you could produce a short but complete program demonstrating the problem, that would really help. – Jon Skeet Jul 08 '12 at 17:33
  • to prevent `I'm not sure what changed` moments in the future, consider using a version control system such as [mercurial](http://hginit.com/). – Adam Jul 08 '12 at 17:37
  • Thanks, I'm on the good side now (happy git user for almost 2 years), but this is an old project where I wasn't :) – Tamás Szelei Jul 08 '12 at 17:41
  • Hey I'm wondering if you should also override `TryGetMember` it returns true or false, telling the DLR weather the member exists in the instance. – Zasz Jul 08 '12 at 17:43
  • I do have TryGetMember and TrySetMember implemented, but I think they are for property-type things (and they are implemented accordingly). As the script object will accept non-existing properties, they always return true (and get/set the dynamic properties on the player). And breakpoints in them are also not hit. – Tamás Szelei Jul 08 '12 at 17:47
  • I think only option you have right now is to de-compile the compiler generated code and dive into it to understand what might be happening....your code looks OK to me and in fact I have been able to use your code in a stub and successfully run it... – Amit Mittal Jul 09 '12 at 08:26

1 Answers1

0

I guess that if you use dynamic directly it will work. The problem could be that you return it through property. This way works it in WPF databinding. If i give dynamic directly as DataContext, binding works. If I use property as you used I get bindig errors.

kokoska69
  • 11
  • 1