c# - 如何防止在 .NET 中智能加载 DLL?

标签 c# .net dll

我正在处理的程序有问题。它由 2 个 DLL 组成,其中 dll A 引用 dll B。Dll A 包含一个公共(public)方法,其中第一个操作(在实例化 B 中的任何类之前)是检查某个网络位置以查看是否有新版本的 dll B 可用。如果是这样,它会在当前 B 的相同位置下载它,这应该不是问题,因为 B 中没有任何内容被实例化。可悲的是,它是实例化的,所以我得到一个错误,它已经被拥有 A 的进程引用并且无法被替换。

您是否知道它已被引用的原因,是否有任何解决方案可以避免这种情况?

public class L10nReports//Class in DLL A
{
    public L10nReports() //constructor
    {
    }

    //only public method is this class
    public string Supervise(object projectGroup, out string msg)
    {
        //Checks for updates of dll B and downloads it if available. And fails.
        manageUpdate();

        //first instanciation of any class from dll B
        ReportEngine.ReportEngine engine = new ReportEngine.ReportEngine();

        string result = engine.Supervise(projectGroup, out msg);

        return result;
    }

最佳答案

当您的“监督”方法被 JIT 时,B dll 将被加载。这里的问题是 DLL 是在第一次加载 B.dll 中需要某些类型的类型信息时加载的,不是第一次实例化对象时。

因此,在引用 B.dll 中的任何类型之前,以及调用任何使用 B.dll 中的类型的方法之前,您必须检查更新。

public class L10nReports//Class in DLL A
{
    public L10nReports() //constructor
    {
    }


    //only public method is this class
    public string Supervise(object projectGroup, out string msg)
    {
       manageUpdate();
       return SuperviseImpl(projectGroup, out msg);
    }


    private string SuperviseImpl(object projectGroup, out string msg)
    {
        //first instanciation of any class from dll B
        ReportEngine.ReportEngine engine = new ReportEngine.ReportEngine();

        string result = engine.Supervise(projectGroup, out msg);

        return result;
    }

关于c# - 如何防止在 .NET 中智能加载 DLL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5421876/

相关文章:

c# - C++ API 返回一个结构数组

c# - ASP.NET Core 如何将任何类型转换为 Controller 操作的 ActionResult<T> 返回类型?

c# - 添加 "Microsoft Access Database Engine 2010 Redistributable"作为 clickonce 的先决条件

c# - 我们如何检查列表框中的任何项目是否在 ASP.NET 2.0 中被选中?

.net - 使用 DotNetOpenAuth(和谷歌应用程序)创建单点登录真正需要什么?

c++ - 从托管 C++ 引用非托管 C++ DLL

c# - Stylecop 告诉我添加这个关键字,但它是多余的 - 对性能有影响吗?

c# - ListViewItem MouseDoubleClick MVVM方式

.net - Ansicon 和 cucumber : 'Ruby interpreter (CUI) 1.9.3p194 [1386-mingw] has stopped working'

c++ - 使用 DLL 的简单方法