我正在处理的程序有问题。它由 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/