c# - 将 intPtr 添加到 int 在 .net Framework 3.5 上生成错误

标签 c# unmanaged unsafe

我有这个代码:

lvItem.pszText = (IntPtr)(lpRemoteBuffer + Marshal.SizeOf(typeof(LV_ITEM)));

它在 4.0 上运行良好。

如果我将项目降级到 3.5,它会给我这个错误:

Operator '+' cannot be applied to operands of type 'System.IntPtr' and 'int'

我知道如何修复它以使其在 3.5 上运行

我不知道为什么它在 4.0 中有效?

提前致谢

最佳答案

是的 - 如果您查看 the documentation for the Addition property您会看到该运算符仅在 .NET 4 中引入。顺便说一下,您不需要强制转换。

在 .NET 3.5 上,您可能会使用:

lvItem.pszText = new IntPtr(lpRemoteBuffer.ToInt64() +
                            Marshal.SizeOf(typeof(LV_ITEM)));

当然你需要希望你不是在一个指针超过 int.MaxValue 的 32 位系统上:)

关于c# - 将 intPtr 添加到 int 在 .net Framework 3.5 上生成错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6522571/

相关文章:

c# - 创建一个以 GUID 作为标识符的 C# 数据表对象?

c# - 在 Sharepoint 中停用 Web 部件功能

c# - 抑制 C# 垃圾回收

c++ - .idl 导入接口(interface)中的 coclass 在别处定义?

c# - 为什么这个不安全的代码会抛出 NullReferenceException?

c# - 了解不安全代码及其用途

c# - 使用 C# 自动化 Photoshop

c# - 为什么PostMessage发送小写按键时会发送多个按键?

c# - C++ CLR 在未安装 .net 4.0 时加载 .net 4.0 dll

rust - mem::forget(mem::uninitialized()) 是否定义了行为?