- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
请耐心等待,我花了 30 多个小时来完成这项工作 - 但没有成功。
在我的程序开始时,我在 bytearray 中加载一个程序集 (dll),然后将其删除。
_myBytes = File.ReadAllBytes(@"D:\Projects\AppDomainTest\plugin.dll");
稍后在程序中我创建一个新的 Appdomain,加载字节数组并枚举类型。
var domain = AppDomain.CreateDomain("plugintest", null, null, null, false);
domain.Load(_myBytes);
foreach (var ass in domain.GetAssemblies())
{
Console.WriteLine($"ass.FullName: {ass.FullName}");
Console.WriteLine(string.Join(Environment.NewLine, ass.GetTypes().ToList()));
}
正确列出类型:
ass.FullName: plugin, Version=1.0.0.0, Culture=neutral,PublicKeyToken=null
...
Plugins.Test
...
现在我想在新的 AppDomain 中创建该类型的实例
domain.CreateInstance("plugin", "Plugins.Test");
此调用导致 System.IO.FileNotFoundException
,我不知道为什么。
当我查看 .NET Assemblies -> Appdomain: plugintest
下的 ProcessExplorer 时,我发现该程序集已正确加载到新的 appdomain 中。
我怀疑发生异常是因为在磁盘上再次搜索程序集。但是为什么程序要重新加载呢?
如何使用从字节数组加载的程序集在新的应用程序域中创建实例?
最佳答案
这里的主要问题是认为您可以在主应用程序域中执行代码时实例化插件。
您需要做的是创建一个代理类型,该代理类型在已加载的程序集中定义,但在新 应用程序域中实例化。如果类型的程序集未加载到两个应用程序域中,您不能跨应用程序域边界传递类型。 例如,如果你想像上面那样枚举类型并打印到控制台,你应该从在新应用程序域中执行的代码中执行,而不是从在当前应用程序域中执行的代码中执行 .
因此,让我们创建我们的插件代理,它将存在于您的主程序集中,并将负责执行所有与插件相关的代码:
// Mark as MarshalByRefObject allows method calls to be proxied across app-domain boundaries
public class PluginRunner : MarshalByRefObject
{
// make sure that we're loading the assembly into the correct app domain.
public void LoadAssembly(byte[] byteArr)
{
Assembly.Load(byteArr);
}
// be careful here, only types from currently loaded assemblies can be passed as parameters / return value.
// also, all parameters / return values from this object must be marked [Serializable]
public string CreateAndExecutePluginResult(string assemblyQualifiedTypeName)
{
var domain = AppDomain.CurrentDomain;
// we use this overload of GetType which allows us to pass in a custom AssemblyResolve function
// this allows us to get a Type reference without searching the disk for an assembly.
var pluginType = Type.GetType(
assemblyQualifiedTypeName,
(name) => domain.GetAssemblies().Where(a => a.FullName == name.FullName).FirstOrDefault(),
null,
true);
dynamic plugin = Activator.CreateInstance(pluginType);
// do whatever you want here with the instantiated plugin
string result = plugin.RunTest();
// remember, you can only return types which are already loaded in the primary app domain and can be serialized.
return result;
}
}
上面评论中的几个要点我在这里重申一下:
MarshalByRefObject
,这意味着可以使用远程处理跨应用程序域边界代理对该对象的调用。[Serializable]
。并且还必须是当前加载的程序集中的类型。如果你需要你的插件返回一些特定的对象给你,说 PluginResultModel
那么您应该在由两个程序集/应用程序域加载的共享程序集中定义此类。 CreateAndExecutePluginResult
在当前状态下,但可以通过自己迭代程序集和类型并删除对 Type.GetType
的调用来删除此要求.接下来,您需要创建域并运行代理:
static void Main(string[] args)
{
var bytes = File.ReadAllBytes(@"...filepath...");
var domain = AppDomain.CreateDomain("plugintest", null, null, null, false);
var proxy = (PluginRunner)domain.CreateInstanceAndUnwrap(typeof(PluginRunner).Assembly.FullName, typeof(PluginRunner).FullName);
proxy.LoadAssembly(bytes);
proxy.CreateAndExecutePluginResult("TestPlugin.Class1, TestPlugin, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null");
}
再说一遍,因为它非常重要,我很长一段时间都没有理解这一点:当你在这个代理类上执行一个方法时,比如 proxy.LoadAssembly
这实际上被序列化为一个字符串,并被传递到要执行的新应用程序域。这不是正常的函数调用,您需要非常小心传递给这些方法或从这些方法传递的内容。
关于c# - 从字节数组加载时找不到 AppDomain 程序集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50127992/
例如,我有一个父类Author: class Author { String name static hasMany = [ fiction: Book,
代码如下: dojo.query(subNav.navClass).forEach(function(node, index, arr){ if(dojo.style(node, 'd
我有一个带有 Id 和姓名的学生表和一个带有 Id 和 friend Id 的 Friends 表。我想加入这两个表并找到学生的 friend 。 例如,Ashley 的 friend 是 Saman
我通过互联网浏览,但仍未找到问题的答案。应该很容易: class Parent { String name Child child } 当我有一个 child 对象时,如何获得它的 paren
我正在尝试创建一个以 Firebase 作为我的后端的社交应用。现在我正面临如何(在哪里?)找到 friend 功能的问题。 我有每个用户的邮件地址。 我可以访问用户的电话也预订。 在传统的后端中,我
我主要想澄清以下几点: 1。有人告诉我,在 iOS 5 及以下版本中,如果您使用 Game Center 设置多人游戏,则“查找 Facebook 好友”(如与好友争夺战)的功能不是内置的,因此您需要
关于redis docker镜像ENTRYPOINT脚本 docker-entrypoint.sh : #!/bin/sh set -e # first arg is `-f` or `--some-
我是一名优秀的程序员,十分优秀!