gpt4 book ai didi

c# - 转换 IntPtr 以增加它的替代方法?

转载 作者:太空宇宙 更新时间:2023-11-03 11:09:58 27 4
gpt4 key购买 nike

假设我有以下代码:

IntPtr newPtr = new IntPtr( oldPtr.ToInt32() + 12 ); 

有什么办法可以让增加oldPtr的过程更加精简吗?
有什么我想念的或我应该知道的吗?

编辑澄清:我的好奇心在于简化流程本身,我只是想进入不受管理的世界,而且不需要转换所有内容就已经足够困难了。

最佳答案

从 Framework 4.0 开始,您可以使用 Add()Substract()方法。如果您是在较旧的环境下开发,请在您的项目中实现:

public static class IntPtrExtensions
{
#region Methods: Arithmetics
public static IntPtr Decrement(this IntPtr pointer, Int32 value)
{
return Increment(pointer, -value);
}

public static IntPtr Decrement(this IntPtr pointer, Int64 value)
{
return Increment(pointer, -value);
}

public static IntPtr Decrement(this IntPtr pointer, IntPtr value)
{
switch (IntPtr.Size)
{
case sizeof(Int32):
return (new IntPtr(pointer.ToInt32() - value.ToInt32()));

default:
return (new IntPtr(pointer.ToInt64() - value.ToInt64()));
}
}

public static IntPtr Increment(this IntPtr pointer, Int32 value)
{
unchecked
{
switch (IntPtr.Size)
{
case sizeof(Int32):
return (new IntPtr(pointer.ToInt32() + value));

default:
return (new IntPtr(pointer.ToInt64() + value));
}
}
}

public static IntPtr Increment(this IntPtr pointer, Int64 value)
{
unchecked
{
switch (IntPtr.Size)
{
case sizeof(Int32):
return (new IntPtr((Int32)(pointer.ToInt32() + value)));

default:
return (new IntPtr(pointer.ToInt64() + value));
}
}
}

public static IntPtr Increment(this IntPtr pointer, IntPtr value)
{
unchecked
{
switch (IntPtr.Size)
{
case sizeof(Int32):
return new IntPtr(pointer.ToInt32() + value.ToInt32());

default:
return new IntPtr(pointer.ToInt64() + value.ToInt64());
}
}
}
#endregion

#region Methods: Comparison
public static Int32 CompareTo(this IntPtr left, Int32 right)
{
return left.CompareTo((UInt32)right);
}

public static Int32 CompareTo(this IntPtr left, IntPtr right)
{
if (left.ToUInt64() > right.ToUInt64())
return 1;

if (left.ToUInt64() < right.ToUInt64())
return -1;

return 0;
}

public static Int32 CompareTo(this IntPtr left, UInt32 right)
{
if (left.ToUInt64() > right)
return 1;

if (left.ToUInt64() < right)
return -1;

return 0;
}
#endregion

#region Methods: Conversion
public unsafe static UInt32 ToUInt32(this IntPtr pointer)
{
return (UInt32)((void*)pointer);
}

public unsafe static UInt64 ToUInt64(this IntPtr pointer)
{
return (UInt64)((void*)pointer);
}
#endregion

#region Methods: Equality
public static Boolean Equals(this IntPtr pointer, Int32 value)
{
return (pointer.ToInt32() == value);
}

public static Boolean Equals(this IntPtr pointer, Int64 value)
{
return (pointer.ToInt64() == value);
}

public static Boolean Equals(this IntPtr left, IntPtr ptr2)
{
return (left == ptr2);
}

public static Boolean Equals(this IntPtr pointer, UInt32 value)
{
return (pointer.ToUInt32() == value);
}

public static Boolean Equals(this IntPtr pointer, UInt64 value)
{
return (pointer.ToUInt64() == value);
}

public static Boolean GreaterThanOrEqualTo(this IntPtr left, IntPtr right)
{
return (left.CompareTo(right) >= 0);
}

public static Boolean LessThanOrEqualTo(this IntPtr left, IntPtr right)
{
return (left.CompareTo(right) <= 0);
}
#endregion

#region Methods: Logic
public static IntPtr And(this IntPtr pointer, IntPtr value)
{
switch (IntPtr.Size)
{
case sizeof(Int32):
return (new IntPtr(pointer.ToInt32() & value.ToInt32()));

default:
return (new IntPtr(pointer.ToInt64() & value.ToInt64()));
}
}

public static IntPtr Not(this IntPtr pointer)
{
switch (IntPtr.Size)
{
case sizeof(Int32):
return (new IntPtr(~pointer.ToInt32()));

default:
return (new IntPtr(~pointer.ToInt64()));
}
}

public static IntPtr Or(this IntPtr pointer, IntPtr value)
{
switch (IntPtr.Size)
{
case sizeof(Int32):
return (new IntPtr(pointer.ToInt32() | value.ToInt32()));

default:
return (new IntPtr(pointer.ToInt64() | value.ToInt64()));
}
}

public static IntPtr Xor(this IntPtr pointer, IntPtr value)
{
switch (IntPtr.Size)
{
case sizeof(Int32):
return (new IntPtr(pointer.ToInt32() ^ value.ToInt32()));

default:
return (new IntPtr(pointer.ToInt64() ^ value.ToInt64()));
}
}
#endregion
}

并且,在您的示例中,调用:

IntPtr newPtr = oldPtr.Increment(12); 

关于c# - 转换 IntPtr 以增加它的替代方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14392135/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com