title: Calling ToDictionary<K, V> and casting from within a generic method <K, V>
body: <p>I'm getting responses back from a server with metadata information.  I have a few tables I'm trying to load from json.  The code I want to do this with is as follows:</p>
  
  <p><strong>Interface:</strong></p>
  
  <pre><code>public interface DALObject
  {
      public Object Id();
      public Object Instantiate();
  }
  </code></pre>
  
  <p><strong>Class Example:</strong></p>
  
  <pre><code>[System.Serializable]
  public class UnitClass_DAL : DALObject
  {
      public string GOOGLEFU_ID { get; set; }
      public int _id { get; set; }
      public string name { get; set; }
  
      object DALObject.Instantiate()
      {
          return Instantiate();
      }
  
      object DALObject.Id()
      {
          return _id;
      }
  
      public int Id()
      {
          return _id;
      }
  
      public UnitClass Instantiate( )
      {
          UnitClass temp = new UnitClass();
          temp.id = _id;
          temp.name = name;
          return temp;
      }
  }
  </code></pre>
  
  <p><strong>Loading Method Inside My RemoteAPIService class:</strong></p>
  
  <pre><code>public void LoadMetadataResource&lt;DALType, K, V&gt;(IDictionary&lt;K, V&gt; list, string resource) where DALType: DALObject
  {
      ++activeLoads;
      var parameters = new SortedDictionary&lt;string, string&gt;();
      parameters.Add("table", resource);
      SendAPICall("tables", parameters, (successResponse) =&gt;
          {
              list = JsonMapper.ToObject&lt;List&lt;DALType&gt;&gt;(successResponse).ToDictionary(temp =&gt; temp.Id() as K, temp =&gt; temp.Instantiate() as V);
              --activeLoads;
          }, (errorResponse) =&gt;
          {
              --activeLoads;
          });
  }
  </code></pre>
  
  <p>The issue I am having is that I want to call something like this:</p>
  
  <pre><code>//...
  LoadMetadataResource&lt;UnitClass_DAL, int, UnitClass&gt;(GameDataHolder.UnitClass, "UnitClass");
  //...
  </code></pre>
  
  <p>GameDataHolder.UnitClass is of type: Dictionary&lt;int, UnitClass&gt;</p>
  
  <p>The error comes in trying to cast temp.Id() as K, or temp.Instantiate() as V.  I need to load it into a specific data type for GameDataHolder, not a generic base class type.</p>
  
  <p>Is this possible in C#?</p>