Can I use a library which references an unavailable assembly, if it's not used?
I have a library X, which has a class C which used component Y. However, Y isn't a开发者_JS百科vailable in 64 bit, so it's been replaced by component Z, but I want to still use Y when available.
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
Note: C-style Comments due to highlighting errors with vb comments.
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?
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.
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.
精彩评论