在我的一个与系统诊断相关的应用程序中,相关的DLL将在C#中动态加载和卸载.经过一些搜索后,我发现一个单独的DLL无法动态加载其完整的AppDomain.所以我必须创建一个AppDomain并使用该DLL动态卸载.但我找不到任何地方我如何在代码中使用它.我无法显示应用程序代码,因为它违反了公司规则.
有人可以告诉我一些应用程序代码来使用它.我想使用appdomain动态加载和卸载dll并在该dll中调用特定方法,dll没有任何入口点.
谢谢你的回答.Ashutosh说
Ill*_*ati 14
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);
}
Run Code Online (Sandbox Code Playgroud)
至于如何卸载它,你必须卸载AppDomain本身,看看这个
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);
}
Run Code Online (Sandbox Code Playgroud)
谢谢大家,这是我找到问题答案的链接:
另一个 dll 可以使用另一个类动态加载和卸载,该类加载程序集并调用该程序集中的方法... AppDomain.CreateInstanceAndUnwrap 通常需要来自当前项目或当前命名空间的程序集输入。为了解决这个问题,我需要 Assembly.LoadFrom(); 在其他类中使用并创建 AppDomain 并使用链接中给出的 AppDomain 对象创建此类的实例。
谢谢你们的回复。