gpt4 book ai didi

c# - 将在运行时编译的程序集加载到不同的 AppDomain 失败

转载 作者:太空宇宙 更新时间:2023-11-03 15:30:25 28 4
gpt4 key购买 nike

我尝试将在运行时编译的 DLL 加载到不同的 AppDomain 中。当对 system.dll 做同样的工作时,这不起作用。这是我的测试代码:

string sourceCode = "using System;\r\n" +
"[Serializable]\r\n" +
"public class Program1{\r\n" +
" public static void Main1(){\r\n" +
" int i = 100;\r\n" +
" i++;" +
" }\r\n" +
"}";

CSharpCodeProvider provider = new CSharpCodeProvider();
CompilerParameters parameters = new CompilerParameters();
Assembly[] assembliesOfCurrentDomain = AppDomain.CurrentDomain.GetAssemblies();//this.CompilerResults.CompiledAssembly.GetReferencedAssemblies();

for (int runAssembliesInCurrDomain = 0; runAssembliesInCurrDomain < assembliesOfCurrentDomain.Length; runAssembliesInCurrDomain++)
{
try
{
parameters.ReferencedAssemblies.Add(assembliesOfCurrentDomain[runAssembliesInCurrDomain].Location);
}
catch
{
}
}

// True - memory generation, false - external file generation
parameters.GenerateInMemory = false;
parameters.OutputAssembly = "D:\\temp\\123.dll";
parameters.IncludeDebugInformation = true;
parameters.ReferencedAssemblies.Add(Assembly.GetEntryAssembly().Location);

// True - exe file generation, false - dll file generation
parameters.GenerateExecutable = false;
parameters.TreatWarningsAsErrors = true;

CompilerResults results = provider.CompileAssemblyFromSource(parameters, sourceCode);

Assembly own = Assembly.LoadFrom("D:\\temp\\123.dll");
Assembly system = Assembly.LoadWithPartialName("System");

AppDomainSetup appDomainSetup = new AppDomainSetup()
{
PrivateBinPath = @"D:\\temp"
};

AppDomain domain = AppDomain.CreateDomain("hello", AppDomain.CurrentDomain.Evidence, appDomainSetup);
domain.Load(system.GetName()); // works
AppDomain.CurrentDomain.Load(own.GetName()); // works
domain.Load(own.GetName()); // works not

我收到带有以下“FusionLog”的 FileNotFoundException

=== Zustandsinformationen vor Bindung ===
LOG: Benutzer = LIGHTTRANS2\schoening
LOG: DisplayName = 123, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
(Fully-specified)
LOG: Appbase = file:///D:/schoening/Projekte_VL/Testprojekte/Compileing/WindowsFormsApplication1/bin/x64/Debug/
LOG: Ursprünglicher PrivatePath = NULL
Aufruf von Assembly : (Unknown).
===
LOG: Diese Bindung startet im default-Load-Kontext.
LOG: Die Anwendungskonfigurationsdatei wird verwendet: D:\schoening\Projekte_VL\Testprojekte\Compileing\WindowsFormsApplication1\bin\x64\Debug\WindowsFormsApplication1.vshost.exe.config
LOG: Die Computerkonfigurationsdatei von C:\Windows\Microsoft.NET\Framework64\v2.0.50727\config\machine.config wird verwendet.
LOG: Die Richtlinie wird derzeit nicht auf den Verweis angewendet (private, benutzerdefinierte, teilweise oder pfadbasierte Assemblybindung)
LOG: Download von neuem URL file:///D:/schoening/Projekte_VL/Testprojekte/Compileing/WindowsFormsApplication1/bin/x64/Debug/123.DLL.
LOG: Download von neuem URL file:///D:/schoening/Projekte_VL/Testprojekte/Compileing/WindowsFormsApplication1/bin/x64/Debug/123/123.DLL.
LOG: Download von neuem URL file:///D:/schoening/Projekte_VL/Testprojekte/Compileing/WindowsFormsApplication1/bin/x64/Debug/123.EXE.
LOG: Download von neuem URL file:///D:/schoening/Projekte_VL/Testprojekte/Compileing/WindowsFormsApplication1/bin/x64/Debug/123/123.EXE.

对不起,这是德语,明天我会尝试发布英文版本。知道这两个程序集之间有什么区别吗?

DLL 编译成功。如果我在 Assembly own = Assembly.LoadFrom("D:\\temp\\123.dll") 行之前注释掉所有内容并使用在上一次运行中编译的 DLL,我会遇到同样的问题。

编辑:根据建议,我尝试了以下也不起作用的方法。

 Assembly own = Assembly.LoadFrom("D:\\temp\\123.dll");

AppDomainSetup appDomainSetup = new AppDomainSetup() {
PrivateBinPath = @"D:\\temp"
};

//FileStream fs = own.GetFiles(true)[0]; // does not work either
FileStream fs = new FileStream("D:\\temp\\123.dll", FileMode.Open, FileAccess.Read, FileShare.Read);
byte[] rawAssembly = new byte[fs.Length];
fs.Read(rawAssembly, 0, (int)fs.Length);

AppDomain domain = AppDomain.CreateDomain("hello", AppDomain.CurrentDomain.Evidence, appDomainSetup);
domain.Load(rawAssembly);

最佳答案

程序集名称不包含程序集的完整路径 - CLR 无法找到您的“临时”程序集。如果您想将特定程序集 文件 加载到 AppDomain 中,则必须使用 byte[] 重载。

关于c# - 将在运行时编译的程序集加载到不同的 AppDomain 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33943179/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com