我遇到了一个奇怪的情况,在特定的方式下使用C#字典的getter方法会引发参数异常,尽管这 永远不应该 发生。这个问题似乎只在我的电脑上发生。
实际上,我已经找到了一个替代工作的解决方案来解决我的原始问题。但是我真的很想知道为什么原始解决方案不起作用。
我有一个用于Solidworks插件的字典,它跟踪打开的文档及其事件处理程序。它的定义如下:
Solidworks有方法可以检索活动文档。当我尝试使用它来检索活动文档的事件处理程序时,就像这样:
我遇到了这个参数异常:
我发现的替代解决方案是先将活动文档绑定到一个变量上,如下所示:
如果有人能帮我理解这个问题,那将非常好!
一些额外的信息:
根据文档,“ActiveDoc”应该返回一个“对象”,但是智能感知告诉我它是动态的。
如上所述,这仅发生在我的机器上,因此我猜测它在某种程度上是环境性的。
无法正常工作的代码片段直接来自于Solidworks示例文件。
ModelDoc2在名为SolidWorks.Interop.sldworks的程序集中定义,并具有以下定义:
实际上,我已经找到了一个替代工作的解决方案来解决我的原始问题。但是我真的很想知道为什么原始解决方案不起作用。
我有一个用于Solidworks插件的字典,它跟踪打开的文档及其事件处理程序。它的定义如下:
private Dictionary<ModelDoc2, DocumentEventHandler> _openDocs = new Dictionary<ModelDoc2, DocumentEventHandler>();
Solidworks有方法可以检索活动文档。当我尝试使用它来检索活动文档的事件处理程序时,就像这样:
_openDocs[SwApp.ActiveDoc]
我遇到了这个参数异常:
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]''
我发现的替代解决方案是先将活动文档绑定到一个变量上,如下所示:
{{我找到的另一种解决方法是首先将活动文档绑定到一个变量上,如下所示:}}
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
System.Collections.Generic.Dictionary\
2[SolidWorks.Interop.sldworks.ModelDoc2,SWAddIn.DocumentEventHandler]`实例传递给方法。你确定你展示给我们的代码是真正有错误的代码吗?(我没有SW来测试) - Simon Mourierif(_openDocs[SwApp.ActiveDoc].Loaded)
。System.Collections.Generic.Dictionary\
2[SolidWorks.Interop.sldworks.ModelDoc2,SWAddIn.DocumentEventHandler]` 是字典的类型。我猜测这个错误信息是由于该方法被实现为扩展方法,因此将其作为第一个参数(如果有意义的话)。 - Tausif(_openDocs[SwApp.ActiveDoc].Loaded)
是这段真实的代码。没有太多上下文信息。它之前唯一的检查是查看活动文档是否为空。 - Taus