![](/img/trans.png)
[英]Operator + cannot be applied to operands of type IntPtr and int - .Net 3.5
[英]Adding intPtr to int generate error on .net framework 3.5
我有這個代碼:
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 中有效?
提前致謝
是的-如果您查看Addition 屬性的文檔,您會發現該運算符僅在 .NET 4 中引入。順便說一下,您不需要演員表。
在 .NET 3.5 上,您可能會使用:
lvItem.pszText = new IntPtr(lpRemoteBuffer.ToInt64() +
Marshal.SizeOf(typeof(LV_ITEM)));
當然,你需要希望你不在一個指針超過int.MaxValue
的 32 位系統上:)
IntPtr 在 .NET 4.0 之前不支持指針運算。 如果您想以這種方式使用指針,則必須使用真正的指針而不是 IntPtr。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.