title: Dynamic object being 'populated' with property that shouldn't exist in the Type that was passed
body: <p>Alright so I am having an issue with dynamics ... I know they are potentially dangerous but hear me out. Basically I have an object, lets call it Customer.</p>
  
  <pre><code>public class Customer
  {
      public int Id { get; set; }
      public string Type { get; set; }
      public string Value { get; set; }
      public bool IsActive { get; set; }
  }
  </code></pre>
  
  <p>I have another object that inherits from this object. Lets call it ImportantCustomer.</p>
  
  <pre><code>public class ImportantCustomer : Customer
  {
      public int SomeOtherValue { get; set; }
  }
  </code></pre>
  
  <p>I now have a method that does my magic which has to do something to anything it is given. The method is not part of either class above.</p>
  
  <pre><code>public void DoTheMagic(dynamic myObject)
  {
      //My magic here
  )
  </code></pre>
  
  <p>The issue is that a layer up I will be getting the object ImportantCustomer but I want to send it a Customer object. So I do something like this: </p>
  
  <pre><code>var customer = (Customer)importantCustomer;
  </code></pre>
  
  <p>or</p>
  
  <pre><code>Customer customer = importantCustomer;
  </code></pre>
  
  <p>or</p>
  
  <pre><code>ICustomer customer = importantCustomer;
  </code></pre>
  
  <p>Either way of doing it nets me the same result that I don't want. Now I have the object I want to send to my method and everything should be good(a Customer). However, part of the logic in my magic calls another method with an object parameter.</p>
  
  <pre><code>public void SomeOtherMethod(object obj)
  {
      //More magic
  )
  </code></pre>
  
  <p>Inside this method it gets all the properties of the object. Which, when stepping through it, it does, however it also gives me the 'SomeOtherValue' property that is in the ImportantCustomer object, but I passed in my new Customer object, which obviously doesn't implement the property. Why is this property being added? Is there some kind of deep down understanding of how this is done that is causing this? Is there something going on with the reference in the background that is causing this property to show up because of the casting to the Customer type?</p>
  
  <p>If I do it the manual way:</p>
  
  <pre><code>var customer = new Customer()
  {
      Id = importantCustomer.Id,
      Type = importantCustomer.Type,
      Value = importantCustomer.Value,
      IsActive = importantCustomer.IsActive,
  };
  </code></pre>
  
  <p>It works as intended in the sense that I don't get the 'SomeOtherValue' property in the magic.</p>
  
  <p>I am having a hard time finding an answer/reason why. If there is an answer/explanation on here I missed please point me to it. </p>
  
  <p>Thanks.</p>
  
  <p><strong>EDIT 1</strong></p>
  
  <p>I don't understand why the SomeOtherValue property would still be there after the cast. I get that there is referencing going on but after the class if I where to do this:</p>
  
  <pre><code>var customer = (Customer)importantCustomer;
  var value = customer.SomeOtherValue; // ERROR
  </code></pre>
  
  <p>The compiler gets angry because the the property doesn't exist in the Customer type. I am probably just missing something important with how the casting works, I just don't know what it is.</p>
  
  <p><strong>Update</strong></p>
  
  <p>Is there a better way, other than using AutoMapper or something similar to get the Customer object I want so I wouldn't have to write it out manually?</p>
  
  <p>Thanks @Matt Burland for the explanation it was insightful.</p>