.Net何时加载程序集以及如何更改此行为?
对于应用程序,我需要检查Crystal Reports运行时库的可用性。我试过的是:.Net何时加载程序集以及如何更改此行为?,.net,assemblies,loader,.net,Assemblies,Loader,对于应用程序,我需要检查Crystal Reports运行时库的可用性。我试过的是: void CheckCrystal() { try { CrystalDecisions.Windows.Forms.CrystalReportViewer test = new CrystalDecisions.Windows.Forms.CrystalReportViewer(); test.Dispose();
void CheckCrystal()
{
try
{
CrystalDecisions.Windows.Forms.CrystalReportViewer test = new CrystalDecisions.Windows.Forms.CrystalReportViewer();
test.Dispose();
}
catch (System.Exception)
{
PTrace.Error("Some dependences needed to run Crystal Reports are not available.");
throw;
}
}
这不起作用,因为在调用方法之前,调用CheckCrystal的方法中抛出了有关缺少Crystal依赖项的File.IOException。就像.Net知道在需要程序集之前需要它一样。这是真的吗?我怎样才能改变这种行为
提前感谢。这是因为代码是基于每个方法进行JIT的,所以当您第一次尝试调用
CheckCrystal()
时,.NET首先尝试编译它,随后加载所有必需的和尚未加载的程序集
.NET允许您在程序集解析失败时拦截某个时刻。为此,请订阅AppDomain.AssemblyResolve事件。这是因为代码是按每个方法进行JIT的,所以当您第一次尝试调用
CheckCrystal()
时,.NET首先尝试编译它,随后加载所有必需的和尚未加载的程序集
.NET允许您在程序集解析失败时拦截某个时刻。为此,请订阅
AppDomain.AssemblyResolve
事件。您可能需要处理AppDomain.AssemblyResolve
事件。更多信息
一个快速而肮脏的例子:
AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
private static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
if (args.Name == "CrystalReports")
{
PTrace.Error("Some dependences needed to run Crystal Reports are not available.");
}
// return located here assembly here or throw exception, etc
}
您可能希望处理
AppDomain.AssemblyResolve
事件。更多信息
一个快速而肮脏的例子:
AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
private static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
if (args.Name == "CrystalReports")
{
PTrace.Error("Some dependences needed to run Crystal Reports are not available.");
}
// return located here assembly here or throw exception, etc
}
就像.Net知道在需要程序集之前需要它一样。这是真的吗
为了提高启动性能,CLR将延迟加载程序集
手动加载或处理事件
就像.Net知道在需要程序集之前需要它一样。这是真的吗
为了提高启动性能,CLR将延迟加载程序集
手动加载或处理事件