字典Getter中的参数异常

Tau*_*aus 10 c# linq clr dynamic solidworks

我遇到了一种奇怪的情况,即以特定的方式在C#字典上使用getter会产生一个参数异常,即使这种情况永远不会发生.问题似乎只发生在我的电脑上.

实际上我已经找到了解决原始问题的替代工作方案.但是,我真的想要解释为什么原始解决方案不起作用.

我有一个在Addin for Solidworks中使用的字典.它跟踪打开的文档及其事件处理程序.它定义如下:

private Dictionary<ModelDoc2, DocumentEventHandler> _openDocs = new Dictionary<ModelDoc2, DocumentEventHandler>();
Run Code Online (Sandbox Code Playgroud)

Solidworks的有方法来检索活动文档.当我尝试使用它来检索活动文档的事件处理程序时,如下所示:

_openDocs[SwApp.ActiveDoc]
Run Code Online (Sandbox Code Playgroud)

我得到这个ArgumentException:

System.ArgumentException: 'Method 'SWAddIn.DocumentEventHandler 
get_Item(SolidWorks.Interop.sldworks.ModelDoc2)' declared on type
'System.Collections.Generic.Dictionary`2[SolidWorks.Interop.sldworks.ModelDoc2,SWAddIn.DocumentEventHandler]' cannot be called with instance of type
'System.Collections.Generic.Dictionary`2[SolidWorks.Interop.sldworks.ModelDoc2,SWAddIn.DocumentEventHandler]''
Run Code Online (Sandbox Code Playgroud)

我找到的替代解决方案是简单地将活动文档绑定到变量,如下所示:

ModelDoc2 activedoc = SwApp.ActiveDoc;
_openDocs[activedoc]
Run Code Online (Sandbox Code Playgroud)

如果有人能帮助我理解这将是伟大的!

一些额外的信息:

根据文档"ActiveDoc"应该返回一个"对象",但intellisense告诉我它是一个动态的

如上所述,它只发生在我的机器上,所以我猜它在某种程度上是环境的

不起作用的代码片段直接来自Solidworks的示例文件.

ModelDoc2在名为SolidWorks.Interop.sldworks的程序集中定义,具有以下定义:

[CoClass(typeof(ModelDoc2Class))]
[Guid("B90793FB-EF3D-4B80-A5C4-99959CDB6CEB")]
public interface ModelDoc2 : IModelDoc2
Run Code Online (Sandbox Code Playgroud)

如果感兴趣,这是来自excpetion的堆栈跟踪:

   at System.Linq.Expressions.Expression.ValidateCallInstanceType(Type instanceType, MethodInfo method)
   at System.Linq.Expressions.Expression.ValidateAccessor(Expression instance, MethodInfo method, ParameterInfo[] indexes, ReadOnlyCollection`1& arguments)
   at System.Linq.Expressions.Expression.ValidateIndexedProperty(Expression instance, PropertyInfo property, ReadOnlyCollection`1& argList)
   at System.Linq.Expressions.Expression.Property(Expression instance, PropertyInfo indexer, IEnumerable`1 arguments)
   at Microsoft.CSharp.RuntimeBinder.ExpressionTreeCallRewriter.GenerateProperty(EXPRCALL pExpr)
   at Microsoft.CSharp.RuntimeBinder.Semantics.ExprVisitorBase.Visit(EXPR pExpr)
   at Microsoft.CSharp.RuntimeBinder.ExpressionTreeCallRewriter.GenerateLambda(EXPRCALL pExpr)
   at Microsoft.CSharp.RuntimeBinder.Semantics.ExprVisitorBase.Visit(EXPR pExpr)
   at Microsoft.CSharp.RuntimeBinder.ExpressionTreeCallRewriter.Rewrite(TypeManager typeManager, EXPR pExpr, IEnumerable`1 listOfParameters)
   at Microsoft.CSharp.RuntimeBinder.RuntimeBinder.CreateExpressionTreeFromResult(IEnumerable`1 parameters, ArgumentObject[] arguments, Scope pScope, EXPR pResult)
   at Microsoft.CSharp.RuntimeBinder.RuntimeBinder.BindCore(DynamicMetaObjectBinder payload, IEnumerable`1 parameters, DynamicMetaObject[] args, DynamicMetaObject& deferredBinding)
   at Microsoft.CSharp.RuntimeBinder.RuntimeBinder.Bind(DynamicMetaObjectBinder payload, IEnumerable`1 parameters, DynamicMetaObject[] args, DynamicMetaObject& deferredBinding)
   at Microsoft.CSharp.RuntimeBinder.BinderHelper.Bind(DynamicMetaObjectBinder action, RuntimeBinder binder, IEnumerable`1 args, IEnumerable`1 arginfos, DynamicMetaObject onBindingError)
   at Microsoft.CSharp.RuntimeBinder.CSharpGetIndexBinder.FallbackGetIndex(DynamicMetaObject target, DynamicMetaObject[] indexes, DynamicMetaObject errorSuggestion)
   at System.Dynamic.DynamicMetaObject.BindGetIndex(GetIndexBinder binder, DynamicMetaObject[] indexes)
   at System.Dynamic.DynamicMetaObjectBinder.Bind(Object[] args, ReadOnlyCollection`1 parameters, LabelTarget returnLabel)
   at System.Runtime.CompilerServices.CallSiteBinder.BindCore[T](CallSite`1 site, Object[] args)
   at System.Dynamic.UpdateDelegates.UpdateAndExecute2[T0,T1,TRet](CallSite site, T0 arg0, T1 arg1)
   at CortimeSWAddIn.SwAddin.OnPostDocChange() in C:\Users\asdf\Development\SWAdd\SWAddIn\SWAddIn\SwAddin.cs:line 1065
Run Code Online (Sandbox Code Playgroud)

And*_*ewK 0

您可以尝试强制转换它,它似乎没有返回正确的类型。一旦声明了变量,它就会正确解析和转换。

_openDocs[(ModelDoc2)SwApp.ActiveDoc]
Run Code Online (Sandbox Code Playgroud)