我遇到了一个奇怪的情况,在C字典上以特定的方式使用getter会产生一个参数异常,即使它应该never happen。这个问题似乎只发生在我的电脑上。实际上,我已经找到了解决我原来问题的另一种方法。不过,我真的很想弄明白为什么原来的解决方案不起作用。我有在solidworks加载项中使用的词典。它跟踪打开的文档及其事件处理程序。定义如下:private Dictionary<ModelDoc2, DocumentEventHandler> _openDocs = new Dictionary<ModelDoc2, DocumentEventHandler>();solidworks有method来检索活动文档。当我尝试使用它来检索活动文档的eventhandler时,如下所示:_openDocs[SwApp.ActiveDoc]我得到这个论证异常:System.ArgumentException: 'Method 'SWAddIn.DocumentEventHandlerget_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]''我发现的另一种解决方案是简单地将活动DOC绑定到变量,如:ModelDoc2 activedoc = SwApp.ActiveDoc;_openDocs[activedoc]如果有人能帮我理解,那就太好了!一些额外信息:根据文档,“activedoc”应该返回一个“object”,但是intellisense告诉我它是一个动态的如前所述,它只发生在我的机器上,所以我猜它在某种程度上是环保的不起作用的代码片段直接来自solidworks的示例文件。ModelDoc2在名为solidworks.interop.sldworks的程序集中定义,其定义如下:[CoClass(typeof(ModelDoc2Class))][Guid("B90793FB-EF3D-4B80-A5C4-99959CDB6CEB")]public interface ModelDoc2 : IModelDoc2这是expetion的stacktrace,如果您感兴趣的话: 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 最佳答案 每当我看到一个异常,说明不能使用X类型的实例作为类型X时,这表明我有多个版本X加载到内存中并排。运行程序并查看“模块”窗口,查看是否加载了多个版本的solidworks库。如果有的话,那就是你的问题。代码的一部分将x类型解析为a版本,而另一部分期望x类型实例的代码则期望b版本。通常你可以通过更新所有的东西来指向库的同一版本,或者通过在你的数据库中使用 >来解决它。
08-05 02:21