dimanche 11 novembre 2018

C# Assembly cast to type fails

There is my sample code

public interface ICommonInterface
{
    Task<T> SomeMethod<T>(string params) where T : new();
}

public class ExternalAssembly : ICommonInterface
{
   public ExternalAssembly(string params) {...}
   public async Task<T> SomeMethod<T>(string params) where T : new() {...}
}

So I am loading the assembly from another project which also references ICommonInterface. I have the following code, which fails at the last step

object assemblyInstance;
Assembly assembly;
assembly = Assembly.LoadFile(assemblyFile);
Type type = assembly.GetType("Namespace.ExternalAssembly");
assemblyInstance = Activator.CreateInstance(type, new[] { params });

var myClass = (assemblyInstance as ICommonInterface);

The problem is that myClass is null

The previous version of the code was working flawlessly, here it is (only interface was different)

public interface ICommonInterface
{
    Task<SomeClass> SomeMethod(string params);
}

public class ExternalAssembly : ICommonInterface
{
   public ExternalAssembly(string params) {...}
   public async Task<SomeClass> SomeMethod(string params) {...}
}





Aucun commentaire:

Enregistrer un commentaire