title: How can I store and lookup a Delegate Method in a dictionary based on Type?
body: <p>I have a an application with Multiple <strong>DataGridViews</strong>.  I want to run a similar event on all of them, to check if the Data has changed when a user is Done editing a cell or has selected a different cell.  To do that I've added a method to the <strong>CellEndEdit</strong> and <strong>CellValidating</strong> <em>events</em>.</p>
  
  <p>I've abstracted out the code to where the my change check method signature looks like so:  </p>
  
  <pre><code>ProcessIsChangedCheck&lt;TypedTableBase&lt;DataRow&gt;, DataRow&gt;
                       (dgv, validatingEventArgs.ColumnIndex,
                        validatingEventArgs.RowIndex,
                        getOriginal);    
  </code></pre>
  
  <p><strong>getOriginal</strong> is a delegate along the lines of <code>Func&lt;Decimal, TypedTableBase&lt;DataRow&gt;&gt;</code> </p>
  
  <p>The idea is I just want to add the same event to each table and have it lookup the <strong>getOriginal</strong> method based on the type of data the <em>DataGridView</em> is displaying.</p>
  
  <p>I've tried the following Dictionary approach:</p>
  
  <pre><code>//private Dictionary&lt;Type, Func&lt;Decimal, TypedTableBase&lt;DataRow&gt;&gt;&gt; _getOriginalLookup;
  private Dictionary&lt;Type, Func&lt;Decimal, dynamic&gt;&gt; _getOriginalLookup;
  
  private Func&lt;Decimal, TypedTableBase&lt;DataRow&gt;&gt; LookupGetOriginal(Type type)
  {
     //lazy load the lookuo table
     if (null == _getOriginalLookup)
     {
        Dictionary&lt;Type, Func&lt;Decimal, TypedTableBase&lt;DataRow&gt;&gt;&gt; orig =
                  new Dictionary&lt;Type, Func&lt;Decimal, TypedTableBase&lt;DataRow&gt;&gt;&gt;(3)
                      {
                         {
                            typeof (csedformsDataSet.AREARow),
                            //(Func&lt;Decimal, TypedTableBase&lt;DataRow&gt;&gt;)
                            aREATableAdapter.GetAreaForID
                         },
                         {
                            typeof (csedformsDataSet.FIELDRow),
                            //(Func&lt;Decimal, TypedTableBase&lt;DataRow&gt;&gt;)
                            fIELDTableAdapter.GetForFieldID
                         },
                         {
                            typeof (csedformsDataSet.PROMPTRow),
                            //(Func&lt;Decimal, TypedTableBase&lt;DataRow&gt;&gt;)
                            pROMPTTableAdapter.GetByID
                         }
                      };
                         /*,
                         {
                            typeof (csedformsDataSet.DIARYRow),
                            //(Func&lt;Decimal, TypedTableBase&lt;DataRow&gt;&gt;)
                            dIARYTableAdapter.GetByID
                         }*/
  
        _getOriginalLookup = orig;
     }
     return (Func&lt;Decimal, TypedTableBase&lt;DataRow&gt;&gt;) _getOriginalLookup[type];
  }
  </code></pre>
  
  <p>However that leads to casting errors.</p>
  
  <p><strong>NOTE:</strong><br>
  The methods each return a different type roughly
  <code>NamedDataTable : TypedTableBase&lt;NamedRow&gt; where NamedRow : DataRow</code> </p>
  
  <p>Form what I can tell the problem seems to stem from the nested types on <strong>TypedTableBase</strong>.</p>
  
  <p>With a dictionary implementation I've described my Event handlers look like so:  </p>
  
  <pre><code>private void CheckIfCellChanged(object sender, 
                                  DataGridViewCellEventArgs e)
  {
     DataGridView dgv = (DataGridView)sender;
  
     DataRowView drv = (DataRowView)dgv.Rows[0].DataBoundItem;
  
     Func&lt;Decimal, TypedTableBase&lt;DataRow&gt;&gt; getOriginal =
     LookupGetOriginal(drv.Row.GetType());
  
     ProcessIsChangedCheck&lt;TypedTableBase&lt;DataRow&gt;, DataRow&gt;
                          (dgv, e.ColumnIndex, e.RowIndex,
                          getOriginal);
  
     Refresh();
  }
  </code></pre>
  
  <p>So How can I properly define my dictionary so it will compile and I lookup and dynamically pass in the getOriginal method based on the data type of the DataGridView Data?</p>