我可以用它引用了一个不可用的组件,如果它不使用图书馆?它不、用它、不可用、组件

2023-09-09 21:18:59 作者:今夜无风无月

我有一个图书馆X,它有一个C类而使用的组件Y. 但是,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

C64.vb

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

注意:的C风格的注释,由于突出的错误用VB意见

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

这个问题,在我看来,是我将有一个参考为Y的64位版本(这是一个COM对象,因此这将是一个互操作程序集,如果有差别)。假设Y的不是来自于code随时随地调用,将我能够实例C 2

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?

推荐答案

您没事在几个层次上。该#如果可以确保您的code不使用任何类型的Y,编译器会实际删除从总装装配参考。即使你确实有在code参考为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对象,并调用它的方法。您可以使用正则的,而不是#如果。让您避免建立单独的程序集。测试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.