类如何直接转化为bytes 没搞清楚你这么干的意义upjf 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 要得到这个字节流干嘛呢,要持久化的话做对象序列话就可以。实在想看内存,VMMap这个工具也许能提供点提示 二进制序列化public byte[] SerializeObject(object pObj) { if (pObj == null) return null; System.IO.MemoryStream _memory = new System.IO.MemoryStream(); BinaryFormatter formatter = new BinaryFormatter(); formatter.Serialize(_memory, pObj); _memory.Position = 0; byte[] read = new byte[_memory.Length]; _memory.Read(read, 0, read.Length); _memory.Close(); return read; } //实例类[Serializable]public class MyObject { public int n1 = 0; public int n2 = 0; public String str = null;}以下代码示例说明该类的实例是如何被序列化到一个文件中的。MyObject obj = new MyObject();obj.n1 = 1;obj.n2 = 24;obj.str = "Some String";IFormatter formatter = new BinaryFormatter();Stream stream = new FileStream("MyFile.bin", FileMode.Create, FileAccess.Write, FileShare.None);formatter.Serialize(stream, obj);stream.Close(); 参考也用C#做个视频监控客户端来玩玩, 一般C++下我们进行网络传输模块开发,会采用结构体来封装组织通信报文,那采用C#开发的客户端程序需与服务端通信,就会碰到怎么把一个结构体转成一个byte数组,以及如何把收到的byte数组数据还原成结构体对象.我们这里就涉及到了C#与其它语言的互操作,也就是所谓的 P/Invoke技术,主要也就是System.Runtime.InteropServices命名空间下的Marshal类的使用.(可参考园友 tuyile006文章struct和byte[]相互转换(用Marshal类实现)) winform配合SQLserver 小白问题 在线等 数据库取值填充到Textbox中 数据库问题 时间判断问题 COM+组件和控件概念有什么不同? 问一个简单的问题 关于串口编程... 修改txt文件 %5Cu9ED1%5Cu5C71 C#里怎么转换啊? 类和方法(菜鸟级别~~) 很郁闷的框架的问题? C#做个资源管理器 转换doc格式到html格式,不需要word安装,100分
实在想看内存,VMMap这个工具也许能提供点提示
public byte[] SerializeObject(object pObj)
{
if (pObj == null)
return null;
System.IO.MemoryStream _memory = new System.IO.MemoryStream();
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(_memory, pObj);
_memory.Position = 0;
byte[] read = new byte[_memory.Length];
_memory.Read(read, 0, read.Length);
_memory.Close();
return read;
}
//实例类
[Serializable]
public class MyObject {
public int n1 = 0;
public int n2 = 0;
public String str = null;
}以下代码示例说明该类的实例是如何被序列化到一个文件中的。
MyObject obj = new MyObject();
obj.n1 = 1;
obj.n2 = 24;
obj.str = "Some String";
IFormatter formatter = new BinaryFormatter();
Stream stream = new FileStream("MyFile.bin", FileMode.Create, FileAccess.Write, FileShare.None);
formatter.Serialize(stream, obj);
stream.Close();