gpt4 book ai didi

c# - 字典 Getter 中的参数异常

转载 作者:可可西里 更新时间:2023-11-01 09:13:28 25 4
gpt4 key购买 nike

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

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

我有一个用于 Solidworks 插件的字典。它跟踪打开的文档及其事件处理程序。它是这样定义的:

private Dictionary<ModelDoc2, DocumentEventHandler> _openDocs = new Dictionary<ModelDoc2, DocumentEventHandler>();

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

_openDocs[SwApp.ActiveDoc]

我得到这个 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]''

我找到的另一种解决方案是先将事件文档简单地绑定(bind)到一个变量,如下所示:

ModelDoc2 activedoc = SwApp.ActiveDoc;
_openDocs[activedoc]

如果有人能帮助我理解那就太好了!

一些额外的信息:

根据文档,“ActiveDoc”应该返回一个“对象”,但智能感知告诉我它是一个动态的

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

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

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

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

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

   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。

您通常可以通过更新所有内容以指向相同版本的库或使用 assembly binding redirects 来解决它。在你的 App.config 中。

关于c# - 字典 Getter 中的参数异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51657617/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com