- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个最小的、可重现的示例有两个问题,该示例具有三个针对 .NET Core 3.1 的项目。但我也想以 .NET Standard 2.0 为目标。
该示例适用于需要在运行时加载程序集并使用提供的程序集的应用程序。
加载的程序集引用了另一个项目,该项目又使用了 NuGet 包。
class Program
{
static void Main(string[] args)
{
var featurePath = System.IO.Path.GetFullPath(@"..\..\..\..\Feature\bin\Debug\netcoreapp3.1\Feature.dll");
var featureAssembly = System.Reflection.Assembly.LoadFile(featurePath);
var featureType = featureAssembly.GetType("SomeFeature");
var featureInstance = System.Activator.CreateInstance(featureType);
featureType.InvokeMember("PrintText",
System.Reflection.BindingFlags.InvokeMethod, null, featureInstance, new object[0]);
}
}
..\Subfeature\Subfeature.csproj
的 ProjectReference并包含文件 SomeFeature.cs:public class SomeFeature
{
public void PrintText()
{
System.Console.WriteLine(new SomeSubfeature().GetText());
}
}
Newtonsoft.Json
的 PackageReference并包含文件 SomeSubfeature.cs:public class SomeSubfeature
{
public string GetText()
{
return Newtonsoft.Json.JsonConvert.SerializeObject("Some Text");
}
}
第一个解决的问题是因为使用的程序集引用了另一个项目和/或使用了包。 Assembly.LoadFrom
仅加载请求的程序集。未加载引用项目的程序集和包。这会导致 FileNotFoundException,因为找不到子特征。
我可以通过 (1) 替换 Assembly.LoadFile(featurePath)
来解决这个问题与 Assembly.LoadFrom(featurePath)
因此也可以加载同一目录中的其他所需的 DLL。并且 (2) 通过添加到 Feature.csproj <PropertyGroup><CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies></PropertyGroup>
中,使仅在发布期间复制到同一目录的包 DLL 在构建期间也被复制。 .
第二个解决的问题是应用程序在加载 DLL 时锁定它们。这使我无法在应用程序仍在运行时部署更新版本的 DLL。它使发布较新版本变得很麻烦,比方说,该应用程序是 IIS 托管应用程序的一部分。 .NET Core 不再支持加载 DLL 的卷影副本。
我可以通过 (1) 替换 Assembly.LoadFile(featurePath)
来解决这个问题进入Assembly.Load(System.IO.File.ReadAllBytes(featurePath))
因此所需的 DLL 是从加载前读取的字节加载的。 (2) 如果目录中的 DLL 文件出现问题,让 filewatcher 重新加载应用程序。
第一个问题的解决方案与第二个问题的解决方案不兼容。 Assembly.LoadFrom
解决了我的第一个问题。 Assembly.Load
解决了我的第二个问题。但是我还没有找到 Assembly.LoadFile
的替代品可以同时解决这两个问题。
最佳答案
将 AssemblyResolve
事件处理程序添加到 AppDomain.CurrentDomain
并像读取所有字节一样处理程序集加载。
const string location = @"..\..\..\..\Dependency\bin\Debug\netcoreapp3.1\";
static void Main(string[] args)
{
var domain = AppDomain.CurrentDomain;
domain.AssemblyResolve += Domain_AssemblyResolve;
var type = Type.GetType("Dependency.DependentClass,Dependency, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null");
var obj = Activator.CreateInstance(type);
Console.WriteLine(obj.ToString());
}
private static Assembly Domain_AssemblyResolve(object sender, ResolveEventArgs args)
{
var dll = $"{args.Name.Split(",")[0]}.dll";
var path = Path.Combine(location, dll);
var asm = Assembly.Load(File.ReadAllBytes(path));
return asm;
}
旁注:您需要为类型提供全名,否则会抛出错误。喜欢:“Dependency.DependentClass”
依赖程序集有2个依赖,一个是Newtonsoft.Json
NuGet包,另一个是另一个项目。 Bar
类驻留在另一个项目中。
public class DependentClass
{
public int MyProperty { get; set; }
public string AnotherProperty { get; set; }
public override string ToString()
{
return JsonConvert.SerializeObject(new Bar());
}
}
编辑: 否决票后,我再次查找上述解决方案,虽然它有效,但似乎更像是 。 net Framework
一种解决方案而不是 .net Core
所以一个更多的。 net Core
之类的解决方案是使用 AssemblyLoadContext
.
Define a custom assembly load context.
public class CustomLoadContext : AssemblyLoadContext
{
private readonly AssemblyDependencyResolver resolver;
public CustomLoadContext(string mainAssemblyToLoadPath) : base(isCollectible: true)
{
resolver = new AssemblyDependencyResolver(mainAssemblyToLoadPath);
}
protected override Assembly Load(AssemblyName name)
{
Console.WriteLine("Resolving : {0}",name.FullName);
var assemblyPath = resolver.ResolveAssemblyToPath(name);
if (assemblyPath != null)
{
return Assembly.Load(File.ReadAllBytes(assemblyPath));
}
return Assembly.Load(name);
}
}
Load your assembly and let the custom loader context to load it's dependencies.
const string location = @"..\..\..\..\Dependency\bin\Debug\netcoreapp3.1\";
static void Main(string[] args)
{
var fullPath = Path.GetFullPath(location + "Dependency.dll");
var clx = new CustomLoadContext(fullPath); // initialize custom context
var asm = clx.LoadFromStream(new MemoryStream(File.ReadAllBytes(fullPath))); // load your desired assembly
var ins = asm.CreateInstance("Dependency.DependentClass");
Console.WriteLine(ins.ToString());
}
关于c# - Assembly.LoadFile、Assembly.LoadFrom 和 Assembly.Load 的替代方案?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59865067/
我正在 div 中加载一个 php 页面,该页面采用发布的变量来显示正确的内容。它的工作原理是这样的: $(".mainArea").load("page.php", {'folder': 'a'})
我是 AWS 新手。我开始学习 ALB 和 NLB。我知道 ALB 在第 7 层协议(protocol)中工作,而 NLB 在第 4 层协议(protocol)中工作。 谁能解释一下ALB和NLB的实
这是来自 this Article . filter(loaded => !loaded) 有什么作用?我没有在任何地方看到该变量的定义。 我明白这个方法的目的是什么,只是不是那一行。 canActi
我应该在 Constructor 还是 FormLoad() 中加载子表单? 我有一些代码调用在控件中嵌入表单的自定义类。我最初是在 Constructor 之外声明我的子窗体,然后在 FormLoa
目录 1、图解 2、json.loads()用法 3、json.load()用法 4、此外还有一种json.dumps 语法
我正在使用 PhoneGap 为 iPad 编写应用程序,我想在不触发 Safari 或使用 ChildBrowser 等内部 Web 浏览器的情况下加载外部 URL。 我正在使用 PhoneGap
人们经常在他们的(与优化和性能相关的)问题和答案中谈论“重载”。 我试图在典型服务器上的常规 Web 应用程序的上下文中量化这一点(以 SO 及其相当小的基础设施为例),假设它们立即返回(以简化和提高
有人可以写这段代码吗: this.Loaded += (s, e) => this.loaded = true; 分成几行代码以便我可以追溯其含义? 在我的代码示例中没有 s 或 e 吗? 最佳答案
我正在使用 jQuery 的 .load()方法和Loading Page Fragments 。以下是我当前的代码: $("#submit").click(function() { $("#
我想实现的是,当单击图像时,在该图像所在的 div 中,加载了一个包含来自另一个 .html 的其他内容的 div,但我无法完成。 如果我这样做,下面的代码将完美运行: $('#windows_lin
我使用 qt 开发了一个 c++ 库。在本文中,我使用 QSqlDatabase 从 SQLite 数据库中查询信息。注意:我的库在 qt 桌面应用程序中运行良好(我在 Linux 上开发)。 现在我
演示:http://jsfiddle.net/FyrRm/1/ 当我们向下滚动时,您会注意到...在滚动到文章标题到 之后我正在展示一个共享小部件。我正在使用 $(window).on("load"
我在 html(PC) 中使用的图像正确加载,我使用了 img 标签。我已将此文件连同图像一起保存并发送到我的手机,但它不会加载到手机上。我对图像大小或任何东西没有任何问题。我认为它与图像位置有关。
我将 .load() 广泛用于 AJAX 内容。很棒,但如果它做得更多一点,我会喜欢它: 如果为了响应用户操作,我多次调用同一个 div 上的 .load(),我可能会在 div 中得到错误的数据。当
我知道很多方法需要调用它的父类(super class)方法,有些方法不需要, 我正在寻找关于方法调配的东西。它在加载方法中初始化,并且在教程中没有[super load]。 我想知道是不是错了,还是
几个月来,我一直在使用pyGame 2.0.1。今天,我升级到最新版本(2.1.2)后,在尝试加载音频文件时开始出现以下错误:。到目前为止我尝试过的东西:。我使用的是Windows 10、Python
我有一个完整的 angular 应用程序,它使用预先加载。 我想将其转换为延迟加载,但是因为我对所有路线都有保护,而且所有路线都是到一条 protected 主路线的子路线,我不知道是否可以做到这一点
我有一个 React 应用程序,它在 useEffect 中调用我的 API,返回一个用作 imy 图像 src 的 URL 列表。 我正在使用 react-loader-spinner 在加载图像时
我正在使用 Slick.js 逐步加载我的图像我注意到有些图像要么部分加载,要么根本没有加载。 例如,在this site上,有两个画廊:建筑和设计(在导航中)。当用户单击任一图像时,他们会看到该图库
我在我的一个项目中收到此警告。这在调用我的后端 api 时会导致问题,因为它调用了 api 两次。我已经尝试过之前在论坛上发布的关于相同查询的解决方案,但我无法解决这个问题。如果有人能帮助我解决这个问
我是一名优秀的程序员,十分优秀!