Hello.
I'm having a little trouble with some casts that i cant figure out out to solve.
I have this interface and the following implementations:
public interface IConfigureServiceOnServer
{
void Configure(Service service);
}
public sealed class ServiceConfigurator : IConfigureServiceOnServer
{
...
}
I'm loading these types at runtime using a dictionary (that holds the assembly and type names) with the following method:
public static T Resolve<T>(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;
}
I get an invalid cast exception when calling it like this
Resolver.Resolve<IConfigureServiceOnServer>("serviceconfiguration", "");
I can't quite figure out why i get this exception because the same code works for the next interface and its implementation:
public interface IServiceManager
{
void Create();
void Configure();
}
public sealed class Service : IServiceManager
{
...
}
Any ideas why the same code works for the 2nd example and not for the first?
Thanks in advance.
EDIT:
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.
EDIT2: This is the exception message:
Unable to cast object of type 'Codegarten.Controller.Configuration.Service.ServiceConfigurator' to type 'Codegarten.Controller.Configuration.IConfigureServiceOnServer '.
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:
>instance.GetType().GetInterfaces()
{System.Type[1]}
[0]: {Name = "IConfigureServiceOnServer" FullName = "Codegarten.Controller.Configuration.IConfigureServiceOnServer"}
SOLUTION
Ok, the problem was caused by the Assembly.LoadFrom method, which loaded the assembly into a different context than the applications.
I solved this issue by using Assembly.Load instead.