C#程序集强制类型转换失败

时间:2018-11-11 18:37:30

标签: c# reflection

有我的示例代码

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() {...}
}

因此,我正在从另一个引用ICommonInterface的项目中加载程序集。我有以下代码,在最后一步失败

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);

问题是myClass为空

以前的代码版本可以正常工作,就在这里(只有界面有所不同)

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

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

0 个答案:

没有答案