title: Generics casting an unknown type to another
body: <p>I'm rewording my question to be more specific and target the actual area of concern.</p>
  
  <p>I have many different classes which are my entities, I do not what is in that class. When I want to upgrade the entities to new ones, I want to transfer the properties to the new ones which are called the same (and as I have found out, they are case insensitive).</p>
  
  <p>My Problem is simple, but probably requires a complex answer.</p>
  
  <p>When I come across a type which is different to the upgraded one I need to cast the old one to the new one. Both types are unknown, because thats the point. I can pass through any two classes I want and it will transfer the properties fine.</p>
  
  <p>So if I had two classes:</p>
  
  <pre><code>public class OldEntity
  {
      public string Name {get;set;}
      public int SomeProperty {get;set;}
  }
  
  public class NewEntity
  {
      public string Name {get;set;}
      public string SomeProperty {get;set;}
  }
  </code></pre>
  
  <p>So I need to find out what the old type of <code>SomeProperty</code>, then cast it to the new type of <code>SomeProperty</code>.</p>
  
  <p>What I thought I could do, and I hope there is a more generic way of doing it:</p>
  
  <pre><code>switch (typeof(SomeProperty.Value.GetType())
  {
      case typeof(Int):
          return Int.Parse(OldSomeProperty.Value);
  }
  </code></pre>
  
  <p>Obviously that is very raw code but you get the gist.</p>
  
  <p>Please can someone give me a more generic way of doing that, one more thing is I have AutoMapper downloaded, it isn't commented so if anyone could tell me where and how it does it that would be a good answer as well.</p>
  
  <p>So I've tried to simple work around, not very generic:</p>
  
  <pre><code>public static object ConvertSourceObjectDestinationObject(object source, object destination)
  {
      // get the string representation of the source object
      var strRepresentation = source.ToString();
      // find out the destinations type
      var originalCode = AssemblyCode(destination.GetType().Assembly.ToString());
  
      // search for a match then convert the source object to the new destination objects type
      switch (originalCode)
      {
          case 0:
              return strRepresentation;
          case 1:
              return int.Parse(strRepresentation);
          case 2:
              return decimal.Parse(strRepresentation);
          case 3:
              return DateTime.Parse(strRepresentation);
          case 4:
              return byte.Parse(strRepresentation);
          case 5:
              return float.Parse(strRepresentation);
          case 6:
              return Guid.Parse(strRepresentation);
          default:
              return strRepresentation;
      }
  }
  
  public static int AssemblyCode(string assemblyString)
  {
      if (assemblyString.Equals(typeof(string).Assembly.ToString()))
          return 0;
  
      if (assemblyString.Equals(typeof(int).Assembly.ToString()))
          return 1;
  
      if (assemblyString.Equals(typeof(decimal).Assembly.ToString()))
          return 2;
  
      if (assemblyString.Equals(typeof(DateTime).Assembly.ToString()))
          return 3;
  
      if (assemblyString.Equals(typeof(byte).Assembly.ToString()))
          return 4;
  
      if (assemblyString.Equals(typeof(float).Assembly.ToString()))
          return 5;
  
      if (assemblyString.Equals(typeof(Guid).Assembly.ToString()))
          return 6;
  
      return -1;
  }
  </code></pre>
  
  <p>I have done a new one which does a TryParse instead of a Parse to make it more safe, but you get the gist. Its not elegant I know, but if someone could fill me in with how to cast unknown objects to other unknown objects properly that would be great.</p>