title: Why can't I instantiate a generic class inferring types from anonymous objects?
body: <p>Let's say I have some class - hypothetical example:</p>
  
  <pre><code>public class InvalidResponseException&lt;TReq, TResp&gt; : Exception
  {
      public TReq RequestData { get; protected set; }
      public TResp ResponseData { get; protected set; }
  
      public InvalidResponseException(string message, TReq requestData, TResp responseData)
          : this(message, null, requestData, responseData)
      {
      }
  
      public InvalidResponseException(string message, Exception innerException, TReq requestData, TResp responseData)
          : base(message, innerException)
      {
          RequestData = requestData;
          ResponseData = responseData;
      }
  }
  </code></pre>
  
  <p>Okay, class defined... and compiles, no problem.</p>
  
  <p>So lets say somewhere else in my body of code, I want to throw this exception and pass an anonymous object into my exception for one or more of the types.  This should keep it generic enough that I can use it any place where I get unexpected responses from some call in my codebase:</p>
  
  <pre><code>var reqData = new { 
      Context = SomeDataContext,
      Username = SomeUserName,
      ParentID = 54,
      RequestValues = ListOfItemsToGet
  }
  
  var respData = results.ToList();
  
  string exceptionMessage = string.Format("Invalid response data detected. Requested {0} items received {1}.", ListOfItemsToGet.Count(), results.Count());
  
  throw new InvalidResponseException(exceptionMessage, reqData, respData);
  </code></pre>
  
  <p>With most anonymous calls you can use type inference so you don't have to define your types in your method call... but this piece of code won't compile.</p>
  
  <p>If I hold my mouse over the "var" of reqData, the compiler tells me that there's a type defined for it, albeit an odd name nobody could manually assign... so theoretically I would think the type can be inferred from that.  </p>
  
  <p>My initial conclusion was that you can't pass anonymous types to generics, but then you can indeed pass anonymous objects to generics in some fashion:</p>
  
  <pre><code>var BritishExpats = from p in People
                      where p.CountryOfBirth == "United Kingdom" &amp;&amp; p.CountryOfResidents != p.CountryOfBirth
                      select new { FirstName = p.FirstName, LastName = p.LastName }
  </code></pre>
  
  <p>Here "BritishPeople" is an IEnumerable&lt;T&gt; where T is inferred to be 'a</p>
  
  <p>... I can iterate over the resulting IEnumerable of anonymous objects and reference their public properties without any issues...</p>
  
  <pre><code>foreach (var ExPat in BritishExpats)
  {
      Console.WriteLine("{0}, {1}", Expat.LastName, Expat.FirstName);
  }
  </code></pre>
  
  <p>Of course I have to use 'var' in my loop because we don't <em>really</em> know what type "BritishExpats" is.</p>
  
  <p>So I can instantiate some types of classes using types inferred by anonymous objects, but not others...</p>
  
  <p>...is there a rhyme or reason as to what can and cannot be instantiated, inferred and not inferred by anonymous objects?</p>