I have run into a weird situation where using the getter on a C# dictionary in a specific way yields an argument exception even though that should never happen. The issue seems to only happen my computer.
Actually I have already found an alternate working solution to my original problem. However i would really like to undestand the why original solution does not work.
I have dictionary that is used in an Addin for Solidworks. It tracks open documents and their eventhandlers. It is define like this:
private Dictionary<ModelDoc2, DocumentEventHandler> _openDocs = new Dictionary<ModelDoc2, DocumentEventHandler>();
Solidworks has method to retrieve the active document. When i attempt to use it to retrive the eventhandler for the active document like this:
_openDocs[SwApp.ActiveDoc]
i get this 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]''
The alternative solution i found was to simply bind the active doc to a variable first like so:
ModelDoc2 activedoc = SwApp.ActiveDoc;
_openDocs[activedoc]
If anyone could help me understand that would be great!
Some extra info:
According to the documentation "ActiveDoc" is supposed to return an "object" but intellisense tells me it is a dynamic
As mentioned, it only happens on my machine, so i am guessing it is environmental in some way
The snippet of code that doesn't work is directly out of Solidworks' example files.
ModelDoc2 is defined in an assembly called SolidWorks.Interop.sldworks with this definition:
[CoClass(typeof(ModelDoc2Class))]
[Guid("B90793FB-EF3D-4B80-A5C4-99959CDB6CEB")]
public interface ModelDoc2 : IModelDoc2
Here is the stacktrace from the excpetion if that is of interest:
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
Whenever I've seen an exception that says you can't use an instance of type X as type X, this has indicated that I have multiple versions of type X loaded into memory side by side.
Run your program and look at the Modules window and see if there is more than one version of your SolidWorks library loaded. If there is, then that's your problem. One part of your code resolved type X to version A, and the other part of your code that's expecting instances of type X is expecting version B.
You can usually solve it by updating everything to point to the same version of the library or by using assembly binding redirects in your App.config.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With