如何在运行时检测缺少.NET参考? [英] How to detect a missing .NET reference at runtime?

查看:135
本文介绍了如何在运行时检测缺少.NET参考?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序包含引用外部库(SQL Server管理对象)。显然,如果该库不运行时系统present,应用程序仍然有效的只要没有方法从这个库被称为是使用类的。

My application contains references to an external library (the SQL Server Management Objects). Apparently, if the library is not present on the run-time system, the application still works as long as no methods are called that use classes from this library.

问题1:这是指定的行为或只是一个(幸运的)侧的方式对CLR的负载库

要检测的基准是否可访问,我目前使用code是这样的:

To detect whether the reference is accessible, I currently use code like this:

Function IsLibraryAvailable() As Boolean
    Try
        TestMethod()
    Catch ex As FileNotFoundException
        Return False
    End Try
    Return True
End Function

Sub TestMethod()
    Dim srv As New Smo.Server()  ' Try to create an object in the library
End Sub

它的工作原理,但它似乎是相当难看。请注意,只有当TestMethod的是一个单独的方法,否则异常将在开始 IsLibraryAvailable 的(之前在try-catch抛出,即使对象实例化的try-catch块内occurrs)。

It works, but it seems to be quite ugly. Note that it only works if TestMethod is a separate method, otherwise the exception will be thrown at the beginning of IsLibraryAvailable (before the try-catch, even if the object instantiation occurrs within the try-catch block).

问题2:有没有更好的选择

在特别的,我怕像内联函数的优化可以从工作阻止我的code。

In particular, I'm afraid that optimizations like function inlining could stop my code from working.

推荐答案

这是意料之中的,因为在JIT是懒惰的每个方法的水平。请注意,内联是不是一个问题在这里,因为这也是一个JIT的关注,不是一个编译器的关注。

That is expected, since the JIT is lazy at the per-method level. Note that inlining isn't an issue here, since that is also a JIT concern, not a compiler concern.

更好的选择:

  • 确保该应用程序与所有安装需要
  • 在使用ilmerge或类似的创建一个单一的组件(如果可能)

就个人而言,我只是使用第一个选项。

Personally, I'd just use the first option.

这篇关于如何在运行时检测缺少.NET参考?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆