gpt4 book ai didi

c# - C#中使用AppDomain动态加载卸载dll

转载 作者:太空狗 更新时间:2023-10-29 19:58:49 25 4
gpt4 key购买 nike

在我的一个与系统诊断相关的应用程序中,相关的 DLL 将在 C# 中动态加载和卸载。经过一番搜索后,我发现一个单独的 DLL 无法动态加载到完整的 AppDomain 中。所以我必须创建一个 AppDomain 并使用该 DLL 进行动态加载和卸载。但是我找不到任何地方如何在代码中使用它。我无法显示应用程序代码,因为它违反了公司规定。

有人可以告诉我一些应用程序代码来使用它吗?我想使用 appdomain 动态加载和卸载 dll 并调用该 dll 中的特定方法,该 dll 没有任何入口点。

感谢您的回答。阿舒托什

最佳答案

How to: Load Assemblies into an Application Domain

public static void Main()


{
// Use the file name to load the assembly into the current
// application domain.
Assembly a = Assembly.Load("example");
// Get the type to use.
Type myType = a.GetType("Example");
// Get the method to call.
MethodInfo myMethod = myType.GetMethod("MethodA");
// Create an instance.
object obj = Activator.CreateInstance(myType);
// Execute the method.
myMethod.Invoke(obj, null);
}

至于如何卸载,需要自己卸载AppDomain,见this

AppDomain Temporary = AppDomain.CreateDomain("Temporary");
try
{
Gateway Proxy =
(Gateway) Temporary.CreateInstanceAndUnwrap("Shim", "Shim.Gateway");

Match M = Proxy.LoadAndMatch("Plugin.dll",
"Though the tough cough and hiccough, plough them through");
}
finally
{
AppDomain.Unload(Temporary);
}

关于c# - C#中使用AppDomain动态加载卸载dll,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6578170/

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