Can I use a library which references an unavailable assembly, if it's not used?

二次信任 提交于 2019-12-12 04:10:53

问题


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.

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?


回答1:


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.



来源:https://stackoverflow.com/questions/3749942/can-i-use-a-library-which-references-an-unavailable-assembly-if-its-not-used

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!