在 .NET 编译时检查引用/类型是否存在

发布于 2024-07-23 09:53:47 字数 261 浏览 4 评论 0原文

我最近发现需要在编译时检查是否:a)某个程序集引用存在并且可以成功解析,或者 b)某个类(其完全限定名称已知)被定义为。 这两种情况对于我的目的来说是等效的,因此能够检查其中一种就足够了。 在 .NET/C# 中有什么办法可以做到这一点吗? 预处理器指令最初让我觉得可能有帮助,但它似乎没有必要的功能。

当然,在运行时检查类型是否存在可以很容易地完成,但不幸的是,这并不能解决我在这种情况下的特定问题。 (我需要能够忽略缺少某个引用的事实,从而回退到代码中的另一种方法。)

I've recently found the need to check at compile-time whether either: a) a certain assembly reference exists and can be successfully resolved, or b) a certain class (whose fully qualified name is known) is defined. These two situations are equivalent for my purposes, so being able to check for one of them would be good enough. Is there any way to do this in .NET/C#? Preprocessor directives initially struck me as something that might help, but it seems it doesn't have the necessary capability.

Of course, checking for the existence of a type at runtime can be done easily enough, but unfortunately that won't resolve my particular problem in this situation. (I need to be able to ignore the fact that a certain reference is missing and thus fall-back to another approach in code.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦在深巷 2024-07-30 09:53:47

是否有原因无法添加引用,然后对程序集中的类型使用 typeof 表达式来验证它是否可用?

var x = typeof(SomeTypeInSomeAssembly);

如果包含 SomeTypeInSomeAssembly 的程序集未被引用且可用,则将无法编译。

Is there a reason you can't add a reference and then use a typeof expression on a type from the assembly to verify it's available?

var x = typeof(SomeTypeInSomeAssembly);

If the assembly containing SomeTypeInSomeAssembly is not referenced and available this will not compile.

春夜浅 2024-07-30 09:53:47

听起来您希望编译器忽略一个代码分支,这实际上只能通过将其隐藏在 #if 块后面来实现。 定义编译器常量并使用 #if 可以满足您的目的吗?

#if MyConstant
.... code here that uses the type ....
#else
.... workaround code ....
#endif

另一种选择是在编译时根本不依赖于其他类,并使用反射或 .NET 4.0 动态关键字来使用它。 如果在 .NET 3.5 或更早版本的性能关键场景中重复调用它,则可以在首次使用时使用 DynamicMethod 来构建代码,而不是每次都使用反射。

It sounds like you want the compiler to ignore one branch of code, which is really only doable by hiding it behind an #if block. Would defining a compiler constant and using #if work for your purposes?

#if MyConstant
.... code here that uses the type ....
#else
.... workaround code ....
#endif

Another option would be to not depend on the other class at compile-time at all, and use reflection or the .NET 4.0 dynamic keyword to use it. If it'll be called repeatedly in a perf-critical scenario in .NET 3.5 or earlier, you could use DynamicMethod to build your code on first use instead of using reflection every time.

强辩 2024-07-30 09:53:47

我似乎在这里找到了解决方案,尽管并不完全符合我最初的希望。

我的解决方案:

我最终做的是创建一个新的构建配置,然后定义一个预编译器常量,我在代码中使用它来确定是否使用引用,或者回退到替代方案(保证工作)方法。 它不是全自动的,但它相对简单并且看起来相当优雅 - 足以满足我的目的。

替代方案:

如果您想完全自动化此操作,可以使用预构建命令来完成,该命令运行批处理脚本/小程序来检查计算机上给定引用的可用性,然后更新包含预编译器常量的文件。 然而,我认为这比它值得付出的努力更多,尽管如果我有多个需要解决的独立参考(检查可用性),它可能会更有用。

I seem to have found a solution here, albeit not precisely for what I was initially hoping.

My Solution:

What I ended up doing is creating a new build configuration and then defining a precompiler constant, which I used in code to determine whether to use the reference, or to fall back to the alternative (guaranteed to work) approach. It's not fully automatic, but it's relatively simple and seems quite elegant - good enough for my purposes.

Alternative:

If you wanted to fully automate this, it could be done using a pre-build command that runs a Batch script/small program to check the availabilty of a given reference on the machine and then updates a file containing precompiler constants. This however I considered more effort than it was worth, though it may have been more useful if I had multiple independent references that I need to resolve (check availability).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文