title: Argument Exception in Dictionary Getter
body: <p>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 <a href="https://msdn.microsoft.com/en-us/library/9tee9ht2(v=vs.110).aspx?f=255&amp;MSPPError=-2147217396" rel="nofollow noreferrer">never happen</a>. The issue seems to only happen my computer. </p>
  
  <p>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.</p>
  
  <p>I have dictionary that is used in an Addin for Solidworks. It tracks open documents and their eventhandlers. It is define like this: </p>
  
  <pre><code>private Dictionary&lt;ModelDoc2, DocumentEventHandler&gt; _openDocs = new Dictionary&lt;ModelDoc2, DocumentEventHandler&gt;();
  </code></pre>
  
  <p>Solidworks has <a href="http://help.solidworks.com/2017/english/api/sldworksapi/solidworks.interop.sldworks~solidworks.interop.sldworks.isldworks~activedoc.html" rel="nofollow noreferrer">method</a> to retrieve the active document. When i attempt to use it to retrive the eventhandler for the active document like this: </p>
  
  <pre><code>_openDocs[SwApp.ActiveDoc]
  </code></pre>
  
  <p>i get this ArgumentException:</p>
  
  <pre><code>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]''
  </code></pre>
  
  <p>The alternative solution i found was to simply bind the active doc to a variable first like so:</p>
  
  <pre><code>ModelDoc2 activedoc = SwApp.ActiveDoc;
  _openDocs[activedoc]
  </code></pre>
  
  <p>If anyone could help me understand that would be great!</p>
  
  <p>Some extra info: </p>
  
  <p>According to the documentation "ActiveDoc" is supposed to return an "object" but intellisense tells me it is a dynamic</p>
  
  <p>As mentioned, it only happens on my machine, so i am guessing it is environmental in some way</p>
  
  <p>The snippet of code that doesn't work is directly out of Solidworks' example files.</p>
  
  <p>ModelDoc2 is defined in an assembly called SolidWorks.Interop.sldworks with this definition: </p>
  
  <pre><code>[CoClass(typeof(ModelDoc2Class))]
  [Guid("B90793FB-EF3D-4B80-A5C4-99959CDB6CEB")]
  public interface ModelDoc2 : IModelDoc2
  </code></pre>
  
  <p>Here is the stacktrace from the excpetion if that is of interest:</p>
  
  <pre><code>   at System.Linq.Expressions.Expression.ValidateCallInstanceType(Type instanceType, MethodInfo method)
     at System.Linq.Expressions.Expression.ValidateAccessor(Expression instance, MethodInfo method, ParameterInfo[] indexes, ReadOnlyCollection`1&amp; arguments)
     at System.Linq.Expressions.Expression.ValidateIndexedProperty(Expression instance, PropertyInfo property, ReadOnlyCollection`1&amp; 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&amp; deferredBinding)
     at Microsoft.CSharp.RuntimeBinder.RuntimeBinder.Bind(DynamicMetaObjectBinder payload, IEnumerable`1 parameters, DynamicMetaObject[] args, DynamicMetaObject&amp; 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
  </code></pre>