如果未使用,我可以使用引用不可用程序集的库吗? [英] Can I use a library which references an unavailable assembly, if it's not used?

查看:18
本文介绍了如果未使用,我可以使用引用不可用程序集的库吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个库 X,它有一个使用组件 Y 的类 C.但是,Y 在 64 位中不可用,因此它已被组件 Z 替换,但我想在可用时仍使用 Y.

I have a library X, which has a class C which used component Y. However, Y isn't available in 64 bit, so it's been replaced by component Z, but I want to still use Y when available.

所以我想引用 Y 和 Z 然后有

So I would like to reference Y and Z and then have

C.vb:

#If 32bit then
Class C
// implementation of C using Y
End Class
#End If

C64.vb

#If 64bit then
Public Class C
// implementation of C using Z
End Class
#End If

注意:由于使用 vb 注释突出显示错误而导致的 C 样式注释.

Note: C-style Comments due to highlighting errors with vb comments.

在我看来,问题是我将在 64 位版本中引用 Y(它是一个 COM 对象,所以它会是一个互操作程序集,如果这有区别的话).假设 Y 不是从代码中的任何地方调用的,我能实例化 C 吗?

The problem, as I see it, is that I will have a reference to Y in the 64 bit version(it's a COM object, so it would be an interop assembly, if that makes a difference). Assuming Y is not called from anywhere in the code, will I be able to instantiate C?

推荐答案

你在几个层面上都没问题.#if 确保您的代码不使用 Y 中的任何类型,编译器实际上会从最终程序集中删除程序集引用.即使您确实在代码中引用了 Y 类型,它们实际上也可以被 JIT 编译,因为您确实拥有有效的元数据.你已经有了互操作程序集.

You're okay on several levels. The #if ensures that your code doesn't use any types from Y, the compiler will actually remove the assembly reference from the final assembly. Even if you did have references to Y types in your code, they can actually be JIT compiled because you do have valid metadata for it. You've got the interop assembly.

您唯一不能做的就是创建 COM 对象并调用其方法.您可以使用常规 if 而不是 #if.允许您避免构建单独的程序集.测试 IntPtr.Size 是确定您是否在 64 位模式下运行的好方法.

The only thing you can't do is create the COM object and call its methods. You can use a regular if rather than #if. Allowing you to avoid building separate assemblies. Testing IntPtr.Size is a good way to find out if you are running in 64-bit mode.

这篇关于如果未使用,我可以使用引用不可用程序集的库吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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