日期:2014-05-18 浏览次数:20951 次
Node* p = new Node();
public static unsafe byte[] GetBytes<T>(object obj)
{
byte[] arr = new byte[Marshal.SizeOf(obj)];
GCHandle handle = GCHandle.Alloc(obj, GCHandleType.Pinned);
void* pVer = handle.AddrOfPinnedObject().ToPointer();
fixed (byte* parr = arr)
{
*parr = *(byte*)pVer;
}
handle.Free();
return arr;
}
public static unsafe T GetObject<T>(byte[] bytes) where T : new()
{
T rect = new T();
GCHandle handle = GCHandle.Alloc(rect, GCHandleType.Pinned);
void* pVer = handle.AddrOfPinnedObject().ToPointer();
fixed (byte* b = bytes)
{
*(byte*)pVer = *b;
}
handle.Free();
return rect;
}
------解决方案--------------------
你用结构做节点的话,会是复制的方式,但是用类做节点就不会复制了。
Node* p = new Node();
Node是个结构吗?那应该这样写:
Node node; 或者Node node = new Node();
Node*p = &node;
对类的实例不能用&取地址。
------解决方案--------------------
你用类就不要用指针了。
C#的指针不适合你这种场合。
public class Node
{
public string Value { get; set; }
public Node Next;
}