Tamás Szelei
Tamás Szelei

Reputation: 23961

Why my TryInvokeMember is not called?

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?

Upvotes: 0

Views: 577

Answers (1)

kokoska69
kokoska69

Reputation: 11

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.

Upvotes: 0

Related Questions