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

Ant*_*ine 4 .net c# dll

我正在处理一个程序问题.它由2个DLL组成,dll A引用dll B. Dll A包含一个公共方法,其中第一个动作(在实例化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;
    }
Run Code Online (Sandbox Code Playgroud)

Phi*_*eck 6

当您的"Supervise"方法被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;
    }
Run Code Online (Sandbox Code Playgroud)


GvS*_*GvS 5

Jit编译器需要加载Dll B,以便检查/验证Supervise方法.

将对Dll B的调用移到另一个方法中,并阻止此方法被内联([MethodImpl(MethodImplOptions.NoInlining)]).否则,您可能会从调试模式切换到释放模式.

如果我没记错,内联不会用于Debug编译代码,但是发布代码可能会内联被调用的方法,在检查之前使抖动加载Dll B.

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

        return SuperviseHelper(projectGroup, out msg);
    }

    [MethodImpl(MethodImplOptions.NoInlining)]
    public string SuperviseHelper(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;
    }
Run Code Online (Sandbox Code Playgroud)

  • 很好的呼吁无内联 (2认同)