C# 实现系统相关整数
我需要定义一个依赖于系统的整数类型,以便与一些低级库兼容。我已经设置了 x86 和 x64 项目配置,并为它们定义了条件编译符号(IA32 和 INTEL64)。
所以,我想做以下事情:
#if IA32
typedef int SysInt;
typedef uint SysUInt;
#elif INTEL64
typedef long SysInt;
typedef ulong SysUInt;
#endif
但是,由于 typedef 在 C# 中不可用,所以这不起作用。实现这一点的最佳选择是什么?
提前致谢。此致。
I need to define a system-dependent integer type, in order to be compatible with some low-level librarys. I've setup a x86 and x64 Project-Configuration, and defined conditional compilation symbols for them (IA32 and INTEL64).
So, I would like to do the following:
#if IA32
typedef int SysInt;
typedef uint SysUInt;
#elif INTEL64
typedef long SysInt;
typedef ulong SysUInt;
#endif
However, that doesn't work due to typedef is not available in C#. What's the best option to implement this?
Thanks in advance. Best regards.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您需要
IntPtr
和UIntPtr
,它们在 32 位进程中为 32 位,在 64 位进程中为 64 位。由于这些类型自动采用进程的“位数”,因此无需条件编译或两个不同的项目配置即可使用它们。不过,如果您确实想对这些值进行数学运算,则应该将它们转换为
long
或ulong
进行数学运算,然后返回到IntPtr
> 或UIntPtr
将它们传递给您的外部库。You want
IntPtr
andUIntPtr
, which are 32 bits in 32-bit processes and 64 bits in 64-bit processes. Since these types automatically take the process's "bitness", there's no need for conditional compilation or two different projects configurations in order to use them.If you actually want to do math on the values, though, you should cast them to
long
orulong
to do the math and then back toIntPtr
orUIntPtr
to pass them to your external libraries.您可以尝试别名:
You could try aliases:
看来你至少可以做这样的事情:
这只是为你别名类型。我不知道这对你的目的有多大用处,但它确实有用。
It seems you could do something like this, at least:
This just aliases the types for you. I don't know how useful that is for your purposes, but it's something.