C# 实现系统相关整数

发布于 2024-10-22 03:31:41 字数 338 浏览 1 评论 0原文

我需要定义一个依赖于系统的整数类型,以便与一些低级库兼容。我已经设置了 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

蓝海似她心 2024-10-29 03:31:41

您需要 IntPtrUIntPtr,它们在 32 位进程中为 32 位,在 64 位进程中为 64 位。由于这些类型自动采用进程的“位数”,因此无需条件编译或两个不同的项目配置即可使用它们。

不过,如果您确实想对这些值进行数学运算,则应该将它们转换为 longulong 进行数学运算,然后返回到 IntPtr > 或 UIntPtr 将它们传递给您的外部库。

You want IntPtr and UIntPtr, 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 or ulong to do the math and then back to IntPtr or UIntPtr to pass them to your external libraries.

メ斷腸人バ 2024-10-29 03:31:41

您可以尝试别名:

#if IA32
using SysInt = System.Int32;
#elif INTEL64
using SysInt = System.Int64;
#endif

You could try aliases:

#if IA32
using SysInt = System.Int32;
#elif INTEL64
using SysInt = System.Int64;
#endif
冷默言语 2024-10-29 03:31:41

看来你至少可以做这样的事情:

#if IA32
    using SysInt = System.Int32;
    using SysUInt = System.UInt32;
#elif INTEL64
    using SysInt = System.Int64;
    using SysUInt = System.UInt64;
#endif

这只是为你别名类型。我不知道这对你的目的有多大用处,但它确实有用。

It seems you could do something like this, at least:

#if IA32
    using SysInt = System.Int32;
    using SysUInt = System.UInt32;
#elif INTEL64
    using SysInt = System.Int64;
    using SysUInt = System.UInt64;
#endif

This just aliases the types for you. I don't know how useful that is for your purposes, but it's something.

浅浅淡淡 2024-10-29 03:31:41
#if IA32
public struct Sys
{
   public int Int { get; set; }
}
#elif INTEL64
public struct Sys
{
   public long Int { get; set; }
}
#endif
#if IA32
public struct Sys
{
   public int Int { get; set; }
}
#elif INTEL64
public struct Sys
{
   public long Int { get; set; }
}
#endif
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文