title: How to handle objects that look the same but have no shared interface
body: <p>I've written up the nuance and the details <a href="http://anotherdevblog.net/posts/help-these-objects-are-the-same-shape-but-dont-share-an=interface" rel="nofollow noreferrer">here</a>, but here's the gist of what I'm struggling with:</p>
  
  <p>I have a third-party component that returns any one of a variety of POCO objects that basically look the same. By look the same, I mean that if you skimmed through the JSON of the objects, you would assume they are the exact same class. But they aren't the same class. And despite having the same shape, they do not implement an interface or inherit from a base class. Something like this:</p>
  
  <pre><code>// Order V1
  public class ThirdPartyOrderV1
  {
    public decimal Amount { get; set; }
    public ThirdPartyOrderItemV1[] LineItems { get; set; }
    public ThirdPartyOrderAddressV1 BillingAddress { get; set; }
    public ThirdPartyOrderAddressV1 ShippingAddress { get; set; }
    public string SomeV1ThingIDontCareAbout { get; set; }
    public decimal AnotherV1ThingIDontCareAbout { get; set; }
    // ...
  }
  
  public class ThirdPartyOrderItemV1
  {
    public int ItemID { get; set; }
    public int Quantity { get; set; }
    // ...
  }
  
  public class ThirdPartyOrderAddressV1
  {
    public string Name { get; set; }
    public string Street { get; set; }
    // ...
  }
  
  // Order V2
  public class ThirdPartyOrderV2
  {
    public decimal Amount { get; set; }
    public ThirdPartyOrderItemV2[] LineItems { get; set; }
    public ThirdPartyOrderAddressV2 BillingAddress { get; set; }
    public ThirdPartyOrderAddressV2 ShippingAddress { get; set; }
    public object SomeV2ThingIDontCareAbout { get; set; }
    // ...
  }
  
  public class ThirdPartyOrderItemV2
  {
    public int ItemID { get; set; }
    public int Quantity { get; set; }
    // ...
  }
  
  
  public class ThirdPartyOrderAddressV2
  {
    public string Name { get; set; }
    public string Street { get; set; }
    // ...
  }
  </code></pre>
  
  <p>The library has 3 or 4 of these versions. Right now when I get the object from the library, I do type checking on them and pass them to a method to handle the specific type:</p>
  
  <pre><code>public static void Main()
  {
      var parser = new ThirdPartyOrderParser();
      var order = parser.ParseOrder("text");
      if (order is ThirdPartyOrderV1)
      {
          IngestOrderV1(order as ThirdPartyOrderV1);
      }
      else if (order is ThirdPartyOrderV2)
      {
          IngestOrderV2(order as ThirdPartyOrderV2);
      }
      // ...
  }
  </code></pre>
  
  <p>This is not ideal since I end up duplicating my method for each type. I want to have a single method that can handle any arbitrary object that looks like an order (or a line item, or an address, etc).</p>
  
  <p>I know I can't be the only one that has faced this type of issue. I have a few ideas, like casting it to a <code>dynamic</code> object and referencing the properties that way. Or serialize it to JSON and traverse through the [JObject representation](<a href="https://www.newtonsoft.com/json/help/html/QueryingLINQtoJSON.htm" rel="nofollow noreferrer">https://www.newtonsoft.com/json/help/html/QueryingLINQtoJSON.htm</a> (or XML or YAML or something else)</p>
  
  <p>But these solutions still feel brittle and just <em>wrong</em>. Is there a better way to handle this? Maybe even some tool that can generate a class the represents the commonalities of the classes and translates back and forth between the actual class and this "common" class?</p>
  
  <p>Thanks!</p>