在C#中使用AppDomain动态加载和卸载dll

前端之家收集整理的这篇文章主要介绍了在C#中使用AppDomain动态加载和卸载dll前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在我的一个与系统诊断相关的应用程序中,相关的DLL将在C#中动态加载和卸载.经过一些搜索后,我发现一个单独的DLL无法动态加载其完整的AppDomain.所以我必须创建一个AppDomain并使用该DLL动态卸载.但我找不到任何地方我如何在代码中使用它.我无法显示应用程序代码,因为它违反了公司规则.

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

谢谢你的回答.
Ashutosh说

解决方法

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#相关文章