How to use #if to decide which platform is being compiled for in C#

前端 未结 3 1360
死守一世寂寞
死守一世寂寞 2021-02-08 15:28

In C++ there are predefined macros:

#if defined(_M_X64) || defined(__amd64__)
    // Building for 64bit target
    const unsigned long MaxGulpSize = 1048576 * 128         


        
3条回答
  •  礼貌的吻别
    2021-02-08 16:05

    Make sure your design considers whether this needs to be known at compile time or at runtime. If it is compile time, then yes, you will probably need to define a constant. This can be passed on the command line, or if using Visual Studio or MSBUILD, through a configuration. Change the configuration and build again.

    If it is runtime, search through the answers to questions such as How to know a process is 32-bit or 64-bit programmatically... and friends.

    However, it is also possible that this distinction may not matter, depending on the needs of your application. .NET manages its own memory, and nothing is stopping your built-for-x86 assembly from running on a 64-bit machine. If you are interopping with unmanaged code, are there any externals from your library that can tell you what sizes you should be using, instead of assuming?

提交回复
热议问题