title: Explicitly cast base class to derived class
body: <p>Problem: I have 2 types which are result sets of 2 different procedures in DB:
  <em>Proc1Result, Proc2Result</em> (We had to split them - but they are basically the same as comes for input/output)</p>
  
  <p>I then decided that I could make use of an interface to switch between needed procedures on runtime - but this means I would need 1 common type to which i could convert <em>Proc1Result and Proc2Result</em></p>
  
  <p>Just so I do not need to maintain this new class (create all the properties, add/remove if anything changes in DB result) - I derived this class from one of the results:</p>
  
  <pre><code>public class DerivedClassForInterface : Proc1Result {}
  </code></pre>
  
  <p>Then I implemented explicit cast from the 2nd proc which works fine, but when I want to implement explicit cast from base class to derived class - it wont allow me (since it kinda already "does" - but it fails at runtime):</p>
  
  <pre><code>public class DerivedClassForInterface : Proc1Result 
  {
      //ok - and works as expected
      public static explicit operator DerivedClassForInterface(Proc2Result v)
      {
          return new DerivedClassForInterface
          {
              ...
          };
      }
  
      //fail: 'user-defined' conversations to or from a base class are not allowed
      public static explicit operator DerivedClassForInterface(Proc1Result v)
      {
          return new DerivedClassForInterface
          {
              ...
          };
      }
  }
  </code></pre>
  
  <p>so this works:</p>
  
  <pre><code>//result2 is of type Proc1Result
  DerivedClassForInterface castedResult = (DerivedClassForInterface)result2;
  //compiles - works as expected at runtime
  </code></pre>
  
  <p>but this does not:</p>
  
  <pre><code>//result1 is of type Proc1Result
  DerivedClassForInterface castedResult = (DerivedClassForInterface)result1;
  //compiles - conversation fails at runtime
  </code></pre>
  
  <p>So why I can not write my own explicit operator if you can not cast from base class to derived class?</p>
  
  <p>Interesting that compiler allows me to cast from base to derived class, and yet it does not work at runtime.</p>
  
  <p>Probably I will go just for simple functions which will do the "casting" for me. Anyone can suggest a better solution (keep in mind that I would like to keep "DerivedClassForInterface" to obey changes in "Proc1Result" (or "Proc2Result" - doesn't matter))</p>
  
  <p><strong><em>EDIT</em></strong><br>
  <a href="https://stackoverflow.com/a/42491546/2644437">@Peter Duniho</a> - Here the types "Proc1Result" and "Proc2Result" are generated as results from stored procedures (linq2sql). I would like to have a code which I wont need to touch when output of those procedures change (since there are bunch of procedures we need to segment - and implementing new modules could and often does add more output).</p>
  
  <p>Proc1 and Proc2 are basically same stored procedures (they require exactly the same input and provides same output (type-wise not data-wise)). Both of them work with different segments of data and are required to be separate.</p>
  
  <p>Sorry for making this confusing (was at end of my working day...) and not clarifying - the question here actually is:  </p>
  
  <p><strong>Why compiler lets me cast from base to derived class when runtime causes exception? And why I can not implement this casting myself (... because it kinda already does - but it just does not work at runtime?)</strong></p>
  
  <p>So from where I stand - it looks the following:<br>
   - I can not implement this cast because it already exists<br>
   - Yet it is doomed to not work  </p>
  
  <p>Here is "Minimal, Complete, and Verifiable code example" (thanks for the link):</p>
  
  <pre><code>//results from stored procedures in database which got splitted appart (linq 2 sql)
  class Proc1Result { }
  class Proc2Result { }
  //
  
  class DerivedClassForInterface : Proc1Result
  {
      public static explicit operator DerivedClassForInterface(Proc2Result v)
      {
          //this part would be exported in generic function
          var derivedClassInstance = new DerivedClassForInterface();
          var properties = v.GetType().GetProperties();
          foreach (var property in properties)
          {
              var propToSet = derivedClassInstance.GetType().GetProperty(property.Name);
              if (propToSet.SetMethod != null) propToSet.SetValue(derivedClassInstance, property.GetValue(v));
          }
          return derivedClassInstance;
      }
  }
  
  interface IProcLauncher
  {
      DerivedClassForInterface GetNeededData();
  }
  
  class ProcLauncher1 : IProcLauncher
  {
      public DerivedClassForInterface GetNeededData()
      {
          var dataFromDb = new Proc1Result();/*just ilustrative*/
          return (DerivedClassForInterface)dataFromDb;
      }
  }
  
  class ProcLauncher2 : IProcLauncher
  {
      public DerivedClassForInterface GetNeededData()
      {
          var dataFromDb = new Proc2Result();/*just ilustrative*/
          return (DerivedClassForInterface)dataFromDb;
      }
  }
  
  
  class Program
  {
      static void Main(string[] args)
      {
          bool causeInvalidCastException = true;
  
          IProcLauncher procedureLauncher;
          if (causeInvalidCastException) procedureLauncher = new ProcLauncher1();
          else procedureLauncher = new ProcLauncher2();
  
          var result = procedureLauncher.GetNeededData();
          Console.WriteLine("I got here!");
      }
  }
  </code></pre>
  
  <p>The idea was:<br>
   - Not have to change any code if output of procedures change.<br>
   - Decide at runtime which proc to use.<br>
   - Export the convertation part as generic function.<br>
   - Got to be injectable.  </p>
  
  <p>I can solve this - let say - by just 1 generic function which will handle conversation for all cases, but the question is above in bold.</p>