我将一个控制台应用程序移植到从外部库加载类型的.NET核心。完整的.NET Framework使用Type.GetType("typename, assemblyname")
时,程序集位于与可执行文件相同的文件夹中。.net核心1.1:Type.GetType从外部程序集返回null
在.NET Core中,无论放置库的位置如何,它都会返回null。
作为一种变通方法我已经安装了System.Runtime.Loader
包和连接到Resolving
事件迫使从一个完整的路径加载:
AssemblyLoadContext.Default.Resolving += Default_Resolving;
type = Type.GetType(value);
其中代表是:
private static Assembly Default_Resolving(AssemblyLoadContext context, AssemblyName assembly)
{
return context.LoadFromAssemblyPath(Path.Combine(Directory.GetCurrentDirectory(), @"bin\Debug\netcoreapp1.1", $"{assembly.Name}.dll"));
}
的问题是:在加载外部程序集时,.NET内核在哪里查找?
你的问题已经在这里找到答案:http://stackoverflow.com/questions/37895278/how-to-load-assemblies-located-in-a-folder -in-net-core-console-app –
是的,我之前通过了这个问题,但我想知道的是:如果我根本不使用System.Runtime.Loader包,只需要Type .GetType,.NET在哪里查找程序集? – Farlop
@Farlop:我想你问了两个单独的问题 - 弄清楚为什么你的电话无法工作以及.net核心如何查找程序集。在另一个问题中询问后者可能是有意义的。但我为了这个目的,可以指向这个文档:https://github.com/dotnet/core-setup/blob/master/Documentation/design-docs/corehost.md –