你看下这个吧,工作流~~好像是要收费的;
在给个开源的 http://www.javaeye.com/topic/100499
这两个网址也是我一直在关注的~~~希望对你有帮助!
给你2个方法,需要添加引用using System.Runtime.InteropServices
/// <summary>
/// </summary>
/// <param name="sIn">该类型的一个实例</param>
/// <param name="nLen">该类型的长度</param>
/// <returns>字节流</returns>
public static byte[] ToByte(object sIn, int nLen)
{
IntPtr psIn = IntPtr.Zero
byte[] buffer = new byte[nLen]
try
{
psIn = Marshal.AllocHGlobal(nLen)
Marshal.StructureToPtr(sIn, psIn, true)
Marshal.Copy(psIn, buffer, 0, nLen)
}
finally
{
if (psIn != IntPtr.Zero) Marshal.FreeHGlobal(psIn)
}
return buffer
}
//将字节流转成指定类型
public static object ToObject(byte[] rawdatas, Type anytype)
{
int rawsize = Marshal.SizeOf(anytype)
if (rawsize >rawdatas.Length) return null
IntPtr buffer = Marshal.AllocHGlobal(rawsize)//分配指定大小的内存,返回一个指针
Marshal.Copy(rawdatas, 0, buffer, rawsize)
object retobj = Marshal.PtrToStructure(buffer, anytype)
Marshal.FreeHGlobal(buffer)
return retobj
}
例如用的时候:
long l = 888
byte[] bb = ToByte(l,sizeof(long))//结果bb[0]=120bb[1]=3,其它6位为0(我的机器是从低位读取的)
long l2 = (long)ToObject(bb, typeof(long))//结果l2=888还原了
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)