title: Cast interface to a class?
body: <p>I have a cms with a page that contains forms.</p>
  
  <p>I want in the cms to have a dropdown with different class names that all implement a validation interface.</p>
  
  <p>My form handlers contain a process method which I want to validate against the selected validation class.</p>
  
  <p>So I have this interface:</p>
  
  <pre><code>public interface IEligibilityValidation
  {
      bool IsValid();
  }
  </code></pre>
  
  <p>and for example this class</p>
  
  <pre><code>public class ComplexEligibilityValidation : IEligibilityValidation
  {
      public bool IsValid()
      {
          return true; /* complex logic here */
      }
  }
  
  public class SimpleEligibilityValidation : IEligibilityValidation
  {
      public bool IsValid()
      {
          return true; /* less complex logic here */
      }
  }
  </code></pre>
  
  <p>And then I have the form handler that can read the selected class name for the validation.</p>
  
  <p>I am unsure whether my handler should implement IEligibilityValidation as well or whether I can somehow invoke the class using reflection.</p>
  
  <p>For example:</p>
  
  <pre><code>public class SampleFormHandler : IEligibilityValidation
  {
      public SimpleFormHandler(FormViewModel model, INode node)
      {
          this.model = model;
          this.node = node;
  
          eligiblityValidationClass = GetPropertyValue("eligibilityValidation");
  
      }
  
      public SampleProcessResult Process()
      {
              if (!string.IsNullOrEmpty(eligiblityValidationClass))
              {
                  var thisType = Type.GetType(eligiblityValidationClass);
                  var thisInstance = Activator.CreateInstance(thisType);
                  var isValid = ((IEligibilityValidation)thisInstance).IsValid();
                  /* Works however, I need the IsValid to have access to a the FormHandler's properties and injected services */
              }
  
      }
  
      public bool IsValid()
      {
          /* Will Always be ignored */
          return true;
      }
  }
  </code></pre>
  
  <p>What is the most elegant way for doing something like this?</p>
  
  <p>The idea is that different FormHandlers will have a different Validation class assigned to them and when invoked the IsValid method will be able to use the properties of that form.</p>
  
  <p>I am looking for some advanced architecture or something smarter than just require unnecessary code. </p>
  
  <p>Thanks </p>