我的DynamicObject实现如下所示:
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;
}
这就是我使用此对象的实例的方式:
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");
用于工作:),我不确定是什么改变了。调试器没有打破TryInvokeMember调用,我得到RuntimeBinderException
。此类在与运行的程序集不同的程序集中定义(通过从同一解决方案添加对项目的引用,以常规方式引用它)。
在对实例执行调用后,我得到以下堆栈跟踪:
Microsoft.CSharp.RuntimeBinder.RuntimeBinderController。的 SubmitError (Microsoft.CSharp.RuntimeBinder.Errors.CError pError)+ 0x23字节
Microsoft.CSharp.RuntimeBinder.Semantics.LangCompiler。的 SubmitError (Microsoft.CSharp.RuntimeBinder.Errors.CParameterizedError 错误)+ 0x24字节
Microsoft.CSharp.RuntimeBinder.Errors.ErrorHandling。的 ErrorTreeArgs (Microsoft.CSharp.RuntimeBinder.Errors.ErrorCode id,Microsoft.CSharp.RuntimeBinder.Errors.ErrArg [] prgarg)+ 0x53 字节 Microsoft.CSharp.RuntimeBinder.Semantics.MemberLookup。 ReportErrors ()+ 0x6cd字节
Microsoft.CSharp.RuntimeBinder.RuntimeBinder。的 BindCall (Microsoft.CSharp.RuntimeBinder.ICSharpInvokeOrInvokeMemberBinder payload,Microsoft.CSharp.RuntimeBinder.Semantics.EXPR callingObject, Microsoft.CSharp.RuntimeBinder.RuntimeBinder.ArgumentObject [] 参数, System.Collections.Generic.Dictionary 字典)+ 0x206字节 Microsoft.CSharp.RuntimeBinder.RuntimeBinder。的 DispatchPayload (System.Dynamic.DynamicMetaObjectBinder 有效负载,Microsoft.CSharp.RuntimeBinder.RuntimeBinder.ArgumentObject [] 参数, System.Collections.Generic.Dictionary 字典)+ 0xb1字节
Microsoft.CSharp.RuntimeBinder.RuntimeBinder。的 BindCore (System.Dynamic.DynamicMetaObjectBinder 有效载荷, System.Collections.Generic.IEnumerable 参数,System.Dynamic.DynamicMetaObject [] args,out System.Dynamic.DynamicMetaObject deferredBinding)+ 0xbc bytes
Microsoft.CSharp.RuntimeBinder.RuntimeBinder。的绑定(System.Dynamic.DynamicMetaObjectBinder 有效载荷, System.Collections.Generic.IEnumerable 参数,System.Dynamic.DynamicMetaObject [] args,out System.Dynamic.DynamicMetaObject deferredBinding)+ 0x56 bytes
Microsoft.CSharp.RuntimeBinder.BinderHelper。的绑定(System.Dynamic.DynamicMetaObjectBinder action,Microsoft.CSharp.RuntimeBinder.RuntimeBinder binder, System.Collections.Generic.IEnumerable ARGS, System.Collections.Generic.IEnumerable arginfos,System.Dynamic.DynamicMetaObject onBindingError)+ 0x2ca 字节 Microsoft.CSharp.RuntimeBinder.CSharpInvokeMemberBinder。的 FallbackInvokeMember (System.Dynamic.DynamicMetaObject target,System.Dynamic.DynamicMetaObject [] args, System.Dynamic.DynamicMetaObject errorSuggestion)+ 0x77字节
System.Dynamic.DynamicObject.MetaDynamic.BindInvokeMember。的 AnonymousMethod__10 (System.Dynamic.DynamicMetaObject e)+ 0x1b字节
System.Dynamic.DynamicObject.MetaDynamic。的 BindInvokeMember (System.Dynamic.InvokeMemberBinder binder,System.Dynamic.DynamicMetaObject [] args)+ 0xb8个字节 System.Dynamic.InvokeMemberBinder.Bind(System.Dynamic.DynamicMetaObject target,System.Dynamic.DynamicMetaObject [] args)+ 0x36字节 System.Dynamic.DynamicMetaObjectBinder.Bind(object [] args, System.Collections.ObjectModel.ReadOnlyCollection 参数,System.Linq.Expressions.LabelTarget returnLabel)+ 0xea 字节 System.Runtime.CompilerServices.CallSiteBinder的 BindCore >(System.Runtime.CompilerServices.CallSite> site,object [] args)+ 0x80字节 System.Dynamic.UpdateDelegates。的 UpdateAndExecuteVoid3 (System.Runtime.CompilerServices.CallSite site,object arg0,decimal arg1,double arg2)+ 0x30e bytes MyApp.DynCallsite。 MyMethod (字符串str1,十进制数,字符串str2)第96行+ 0x17d字节C#
看起来好像我使用的是DynamicObject实例而不是我专门的派生类。但是,我确认情况并非如此,因为我可以检查调试器中的callsite对象,并说它是DynCallsite。什么可能导致这种情况?
答案 0 :(得分:0)
我想如果你直接使用动态它会起作用。问题可能是你通过财产返回。这种方式适用于WPF数据绑定。如果我直接将动态赋予DataContext,则绑定有效。如果我使用你使用的属性我会遇到bindig错误。