title: Casting object to generic T
body: <p>I'm having a little trouble with some casts that i cant figure out out to solve.</p>
  
  <p>I have this interface and the following implementations:</p>
  
  <pre><code>public interface IConfigureServiceOnServer
  {
      void Configure(Service service);
  }
  
  public sealed class ServiceConfigurator : IConfigureServiceOnServer
  {
   ...
  }
  </code></pre>
  
  <p>I'm loading these types at runtime using a dictionary (that holds the assembly and type names) with the following method:</p>
  
  <pre><code> public static T Resolve&lt;T&gt;(string type, params object[] values) where T : class
          {
              var split = refs[type].Split(new[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries);
              var assembly = Assembly.LoadFrom(split[0] + ".dll");
              var instance = assembly.CreateInstance(
                  split[1], // the name of the Type to instantiate
                  true,
                  BindingFlags.CreateInstance,
                  null,
                  values, // the params to use in the constructor
                  null,
                  null);
              T ret = (T)instance; //also tried with 'instance as T'
              return ret;
          }
  </code></pre>
  
  <p>I get an invalid cast exception when calling it like this</p>
  
  <pre><code>Resolver.Resolve&lt;IConfigureServiceOnServer&gt;("serviceconfiguration", "");
  </code></pre>
  
  <p>I can't quite figure out why i get this exception because the same code works for the next interface and its implementation:</p>
  
  <pre><code>public interface IServiceManager
  {
      void Create();
      void Configure();
  }
  
  public sealed class Service : IServiceManager
  {
  ...
  }
  </code></pre>
  
  <p>Any ideas why the same code works for the 2nd example and not for the first?</p>
  
  <p>Thanks in advance.</p>
  
  <p>EDIT:</p>
  
  <p>When i'm debugging and i stop execution right before the return in the Resolve method i can use the immediate window - i test the cast and it works. </p>
  
  <p>EDIT2:
  This is the exception message:</p>
  
  <pre><code>Unable to cast object of type 'Codegarten.Controller.Configuration.Service.ServiceConfigurator' to type 'Codegarten.Controller.Configuration.IConfigureServiceOnServer '.
  </code></pre>
  
  <p>EDIT3: 
  Some more info - Someone suggested the type loaded using reflection could be wrong so i did a little more debugging. I checked to see if the loaded type implemented the desired interface using the immediate window in visual studio:</p>
  
  <pre><code>&gt;instance.GetType().GetInterfaces()
  {System.Type[1]}
      [0]: {Name = "IConfigureServiceOnServer" FullName = "Codegarten.Controller.Configuration.IConfigureServiceOnServer"}
  </code></pre>
  
  <p><strong>SOLUTION</strong><br>
  Ok, the problem was caused by the Assembly.LoadFrom method, which loaded the assembly into a different context than the applications.</p>
  
  <p>I solved this issue by using Assembly.Load instead.</p>