简体   繁体   English

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

[英]Using AppDomain in C# to dynamically load and unload dll

In one of my application, which is related to system diagnostics, the related DLL is to be loaded and unloaded dynamically in C#.在我的一个与系统诊断相关的应用程序中,相关的 DLL 将在 C# 中动态加载和卸载。 After some search I found that a separate DLL cannot be loaded dynamically its the complete AppDomain.经过一番搜索,我发现无法动态加载单独的 DLL 其完整的 AppDomain。 So I have to create an AppDomain and use that DLL to be loaded unloaded dynamically.所以我必须创建一个 AppDomain 并使用 DLL 动态加载卸载。 But I could not find anywhere how can I use that in code.但我找不到任何地方如何在代码中使用它。 I can not show the app code since it is against company rules.我无法显示应用程序代码,因为它违反了公司规则。

Can somebody tell me some application code to use it.有人可以告诉我一些应用程序代码来使用它。 I want to load and unload the dll dynamically using appdomain and call a specific method in that dll, the dll does not have any entry point.我想使用appdomain动态加载和卸载dll并调用dll中的特定方法,dll没有任何入口点。

Thanks for answers.感谢您的回答。 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);
    }

As for how to unload it, you have to unload the AppDomain itself, see this至于怎么卸载,就得自己卸载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);
}

It's difficult to understand your question, but I will try to make some suggestions.很难理解你的问题,但我会尽力提出一些建议。

There is no reason you cannot dynamically Load a dll directly into your application w/oa separate App Domain, the trick is that you cannot unload it.没有理由您不能将 dll 直接动态加载到您的应用程序中,而无需单独的应用程序域,诀窍是您无法卸载它。 This is only important if you may load multiple versions of the same dll (ie you want the ability to update this diagnostic component to a new version without halting the execution of your application).仅当您可以加载相同 dll 的多个版本时,这一点才重要(即,您希望能够在不停止执行应用程序的情况下将此诊断组件更新到新版本)。 If that is what you are trying to do, then I suggest this CodeProject article .如果那是您想要做的,那么我建议您阅读此 CodeProject 文章

Actually you can dynamically load assemblies into your app domain and run code from it, the issue is that you cannot then unload the assembly.实际上,您可以将程序集动态加载到您的应用程序域中并从中运行代码,问题是您无法卸载程序集。 You can however load additional app domains (and assemblies into them) and unload the app domain when you are done.但是,您可以加载其他应用程序域(以及其中的程序集)并在完成后卸载应用程序域。

As its name suggests though, you then have a new application domain, and you can't just simply call its code and use its types you need to marshal your calls and data across the domain boundaries.正如它的名字所暗示的那样,你有了一个新的应用程序域,你不能只是简单地调用它的代码并使用它的类型,你需要跨域边界来编组你的调用和数据。 If you search you will find lots of examples on how to do this.如果您搜索,您会发现很多关于如何执行此操作的示例。

Something to consider though, is that this is a common pattern, and there are ready made solutions for it, the framework itself has a whole addin namespace that is dedicated to this type of plug-in behavior, it might be worth your while in having a close look at that first.不过要考虑的是,这是一种常见的模式,并且有现成的解决方案,框架本身有一个完整的插件命名空间,专门用于这种类型的插件行为,可能值得你花时间先仔细看看。 There is an article here that shows how to use it. 这里有一篇文章展示了如何使用它。

Thanks guys, here is link where i found answer to my quetion:谢谢大家,这是我找到问题答案的链接:

The MSDN forum description for load and unload of assemblies dynamically 动态加载和卸载程序集的 MSDN 论坛描述

The other dll can be dynamically loaded and unloaded using another class which does load assembly and and call methods in that assembly... AppDomain.CreateInstanceAndUnwrap generally wants input as assemblies from current project or generally current namespace.另一个 dll 可以使用另一个 class 动态加载和卸载,该 class 加载程序集并在该程序集中调用方法...... AppDomain.CreateInstanceAndUnwrap 通常希望输入作为来自当前项目或通常当前命名空间的程序集。 to solve that i need Assembly.LoadFrom();解决我需要 Assembly.LoadFrom(); to be used in some other class and create AppDomain and create instance of this class using AppDomain object as given in link.用于其他一些 class 并创建 AppDomain 并使用链接中给出的 AppDomain object 创建此 class 的实例。

Thanks for ur replies guys.谢谢你们的回复。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM