简体   繁体   English

DLL中的ResolveEventHandler(类库)

[英]ResolveEventHandler in DLL (class library)

In C# there is ResolveEventHandler event to load external dll s if they are not inside application directory. 在C#中,如果外部dll不在应用程序目录中,则存在ResolveEventHandler事件来加载它们。

To use it in winform application I register the event in Program.cs Main() function like the following: 为了在winform应用程序中使用它,我在Program.cs Main()函数中注册了事件,如下所示:

AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(ResolveAssembly);

and then there is the ResolveAssembly function that gets called every time the event is fired: 然后每次触发事件时都会调用ResolveAssembly函数:

    static Assembly ResolveAssembly(object sender, ResolveEventArgs args)
    {
        //MessageBox.Show(String.Format("Assembly {0} is missing", args.Name));


        //This handler is called only when the common language runtime tries to bind to the assembly and fails.

        //Retrieve the list of referenced assemblies in an array of AssemblyName.
        Assembly MyAssembly, objExecutingAssemblies;
        string strTempAssmbPath = "";
        string AssemblyName = new AssemblyName(args.Name).Name;

        objExecutingAssemblies = Assembly.GetExecutingAssembly();
        AssemblyName[] arrReferencedAssmbNames = objExecutingAssemblies.GetReferencedAssemblies();

        //Loop through the array of referenced assembly names.
        foreach (AssemblyName strAssmbName in arrReferencedAssmbNames)
        {
            //Check for the assembly names that have raised the "AssemblyResolve" event.
            if (strAssmbName.Name == AssemblyName)
            {
                //Build the path of the assembly from where it has to be loaded.                
                strTempAssmbPath = @"C:\PowerVision\libraries\" + AssemblyName + ".dll";
                break;
            }

        }
        //Load the assembly from the specified path.                    
        MyAssembly = Assembly.LoadFrom(strTempAssmbPath);

        //Return the loaded assembly.
        return MyAssembly;
    }

The question is how can I add/call this event in/from class library? 问题是如何在类库中添加/调用此事件?

I have a class library (DLL) that has 3 references to external DLL s. 我有一个类库(DLL),它具有3个对外部DLL的引用。 I don't want to copy these dlls into application directory and don't want to place them into application's subdirectory. 我不想将这些dll复制到应用程序目录中,也不想将它们放入应用程序的子目录中。 These DLLs should stay in a specific external folder (hence using the event). 这些DLL应该保留在特定的外部文件夹中(因此使用该事件)。

The problem is I don't know where in DLL(class library) to put this event registration: 问题是我不知道该事件注册在DLL(类库)中的什么位置:

AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(ResolveAssembly);

You just need to put the event registration in your DLL in a place that gets called sometime BEFORE any of your 3 external DLLs get referenced. 您只需要在3个外部DLL中的任何一个被引用之前的某个时间将事件注册放在DLL中的某个位置即可。

A constructor of an object in your top DLL would be the first place to look. 顶级DLL中的对象的构造函数将是第一个查找的对象。 However, if that object is a sub-type of an object that is in one of those 3 external DLLs, then you may need to create a parent object for that object, and call the parent object first, and add the event registration in that parent's constructor. 但是,如果该对象是这3个外部DLL之一中的对象的子类型,则可能需要为该对象创建一个父对象,并首先调用该父对象,并在其中添加事件注册。父级的构造函数。

Eg if your DLL is a UserControl that is based on another UserControl that is in one of those 3 external DLL's, like this: 例如,如果您的DLL是基于这3个外部DLL之一中的另一个UserControl的UserControl,如下所示:

public partial class TopLevelUserControl: ExternalDllUserControl
{
    InitializeComponent();
}

then you may need to write code like this: Create a new user control called TopLevelUserControlLauncher, and place TopLevelUserControl in that userControl, docked. 那么您可能需要编写如下代码:创建一个名为TopLevelUserControlLauncher的新用户控件,并将TopLevelUserControl放置在已停靠的userControl中。 Then write code like this: 然后编写如下代码:

public partial class TopLevelUserControlLauncher: UserControl
{
    AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(ResolveAssembly);
    InitializeComponent(); // this will construct TopLevelUserControl
}

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

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