自定义序列化技术3 (.net 序列化技术) C++ 调用C# DLL
打开SerializableAttribute利用里面的函数进行编辑。
// sparse.cpp : 定义控制台应用程序的入口点。
// #include "stdafx.h"
#include <conio.h>
#using <system.dll>
#using <system.messaging.dll>
#using <System.Runtime.Serialization.Formatters.Soap.dll>//这个里面包含二进制流序列化 using namespace System;
using namespace System::IO;
using namespace System::Runtime::Serialization::Formatters::Binary;
// A test object that needs to be serialized. [Serializable]
ref class TestSimpleObject
{
private:
int member1;
String^ member2;
String^ member3;
double member4; public: // A field that is not serialized. [NonSerialized]
String^ member5; TestSimpleObject()
{
member1 = ;
member2 = "hello";
member3 = "hello";
member4 = 3.14159265;
member5 = "hello world!";
} void Print()
{
Console::WriteLine( "member1 = ' {0}'", member1 );
Console::WriteLine( "member2 = ' {0}'", member2 );
Console::WriteLine( "member3 = ' {0}'", member3 );
Console::WriteLine( "member4 = ' {0}'", member4 );
Console::WriteLine( "member5 = ' {0}'", member5 );
} }; int main()
{ //Creates a new TestSimpleObject object.
TestSimpleObject^ obj = gcnew TestSimpleObject;
Console::WriteLine( "Before serialization the Object* contains: " );
obj->Print();
//下面是序列化与反序列化过程
Stream^ stream = File::Open( "data.xml", FileMode::Create );
Stream^ stream = gcnew MemoryStream();
BinaryFormatter^ formatter = gcnew BinaryFormatter();
formatter->Serialize( stream, obj );
stream->Position = ;
array<Byte>^ buffer = gcnew array<Byte>(stream->Length); //定位到stream里面的Read方法,找到Read方法在这里面该怎么用。
stream->Read( buffer,,sizeof(buffer)); //使用 Read 读取数据块
stream->Flush();
stream->Close();
//Empties obj.
obj = nullptr; //Opens file S"data.xml" and deserializes the object from it.
stream = File::Open( "data.xml", FileMode::Open );
// formatter = gcnew SoapFormatter; formatter = gcnew BinaryFormatter();
obj = dynamic_cast<TestSimpleObject^>(formatter->Deserialize( stream ));
stream->Close();
Console::WriteLine( "" );
Console::WriteLine( "After deserialization the object contains: " );
obj->Print();
getch();
return ;
}
选中“解决方案”下的项目,点击“项目”-“引用”-“添加引用…”-“浏览”,现则使用的dll文件
在项目“属性”-“常规”-“公共语言运行库支持”中选择“公共语言运行库支持/clr” 。这个很重要。
在代码中使用 #using "system.dll" 来引用该动态库
使用 using namespace system; 来引用其名字空间
访问托管对象时使用“^” System::String ^hostname;可以理解为指针。托管对象的指针。
new对象时请使用 gcnew。
下面调用了.net里面的dll,然后序列化。
//下面是序列化过程
//记得一点,UDP传输的过程中是字节流。 而序列化后是二进制流,必须把二进制流再转换为字节流才可以。
Stream^ stream = gcnew MemoryStream();
BinaryFormatter^ formatter = gcnew BinaryFormatter();
formatter->Serialize( stream, obj );
stream->Position = ;
// Now read s into a byte buffer.
array<Byte>^ buffer = gcnew array<Byte>(stream->Length); //必须定义这种结构,不然,Read函数会不认识。其实是用到了.net里面的array<Byte>
//类型的元素。
int numBytesToRead = (int) stream -> Length;
int numBytesRead = ;
while(numBytesToRead > )
{
int n = stream -> Read(buffer,numBytesRead,stream -> Length); //这里是Read函数的用法,将stream流里面的内容,写入buffer里面去。从0 位置开始写。长度stream -> Length;
if (n == )
{
break;
}
numBytesRead += n;
numBytesToRead -= n;
}
obj = nullptr; //释放内存
stream -> Close(); //关闭数据流
char* pointArray = new char[sizeof(buffer)]; //这是将array<Byte>类型转为char[]类型。但前提是buffer不能为指针,所以,以上的这句代码是错 的。但方法正确。
for(int i=;i<sizeof(buffer);i++) {
pointArray[i]=safe_cast<byte>(buffer[i]); // allows you to change the type of an expression and generate verifiable MSIL code. 27 }
什么是序列化?
将对象的状态信息转换为可以存储或传输的形式的过程。在序列化期间,对象将其当前状态写入到临时或持久性存储区。以后,可以通过从存储区中读取或反序列化对象的状态,重新创建该对象。
哪些可以被序列化?
序列化分为3种:
截止至.NET 4.0总共有如下若干个序列化类
1.xml序列化 XmlSerializer
2.二进制序列化 BinaryFormatter
3.Soap序列化 SoapFormatter
4.WCF序列化 DataContractSerializer
5.Json序列化 DataContractJsonSerializer和JavascriptSerializer
强调一点:
socket 、 udp 、 tcp之间的区别。
socket只是一种套接字,就是两台电脑两端的东西,中间传输以流的格式进行。
数据量大的情况,会多传几个包
能否在短时间内发送完,取决于带宽。也就是说,带宽越大,它在网络中进行传输的时间几乎可以忽略。费得时间主要在数据解压这块,能否实时的解压,清空缓冲区,使得积压变小,这才是关键所在。
自定义序列化技术3 (.net 序列化技术) C++ 调用C# DLL的更多相关文章
- drf序列化高级、自定义只读只写、序列化覆盖字段、二次封装Response、数据库查询优化(断关联)、十大接口、视图家族
目录 自定义只读 自定义只写 序列化覆盖字段 二次封装Response 数据库关系分析 断外键关联关系 ORM操作外键关系 ORM四种关联关系 基表 系列化类其他配置(了解) 十大接口 BaseSer ...
- java基础复习-自定义注解4(结合JDBC技术,打造类表映射微框架)
写在前面: 1.该框架为自己所写的第一个框架类产品,可能有着许多不足的地方,读者可以到评论区指出.同时,该微框架的源码也会开源至博客中,够后来的学习者借鉴.由于该框架逻辑结构稍些复杂,不可能花大量篇幅 ...
- [.net 面向对象程序设计进阶] (13) 序列化(Serialization)(五) Json 序列化利器 Newtonsoft.Json 及 通用Json类
[.net 面向对象程序设计进阶] (13) 序列化(Serialization)(五) Json 序列化利器 Newtonsoft.Json 及 通用Json类 本节导读: 关于JSON序列化,不能 ...
- 字定义JSON序列化支持datetime格式序列化
字定义JSON序列化支持datetime格式序列化 由于json.dumps无法处理datetime日期,所以可以通过自定义处理器来做扩展,如: import json from datetime i ...
- C# 二进制序列化(BinaryFormatter),Xml序列化(XmlSerializer),自己模拟写一个Xml序列化过程。
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- 大前端技术系列:TWA技术+TensorFlow.js => 集成原生和AI功能的app
大前端技术系列:TWA技术+TensorFlow.js => 集成原生和AI功能的app ( 本文内容为melodyWxy原作,git地址:https://github.com/melodyWx ...
- java序列化和反序列化及序列化方式
平时我们在Java内存中的对象,是无 法进行IO操作或者网络通信的,因为在进行IO操作或者网络通信的时候,人家根本不知道内存中的对象是个什么东西,因此必须将对象以某种方式表示出来,即 存储对象中的状态 ...
- 解决Eclipse中文乱码 - 技术博客 - 51CTO技术博客 http://hsj69106.blog.51cto.com/1017401/595598/
解决Eclipse中文乱码 - 技术博客 - 51CTO技术博客 http://hsj69106.blog.51cto.com/1017401/595598/
- hadoop序列化机制与java序列化机制对比
1.采用的方法: java序列化机制采用的ObjectOutputStream 对象上调用writeObject() 方法: Hadoop 序列化机制调用对象的write() 方法,带一个DataOu ...
随机推荐
- hdu6136[模拟+优先队列] 2017多校8
有点麻烦.. /*hdu6136[模拟+优先队列] 2017多校8*/ #include <bits/stdc++.h> using namespace std; typedef long ...
- 一道背包神题-Petrozavodsk Winter-2018. Carnegie Mellon U Contest Problem I
题目描述 有\(n\)个物品,每个物品有一个体积\(v_i\),背包容量\(s\).要求选一些物品恰好装满背包且物品个数最少,并在这样的方案中: (1)求出中位数最小的方案的中位数(\(k\)个元素的 ...
- Idea下maven的配置和使用
maven的主要功能就是依赖管理,jar包仓库.和C#中的NuGet仓库差不多.另外也提供打包构建,启动插件等功能.下面主要讲一下,在使用Idea开发时,maven的配置和使用. maven的安装和配 ...
- js3:数据类型,数组,String各个属性,以及字符串表达式用eval计算
原文发布时间为:2008-11-08 -- 来源于本人的百度文章 [由搬家工具导入] <html> <head> <title>js</title> & ...
- ckeditor编辑的使用方法
一.下载安装Ckeditor,并将其整合到项目中 1.什么是CKeditor?为什么要使用它? 我们在做门户网站或者公文系统时,客户经常要求在录入时能够更改字体样式.大小.颜色并具备插入图片的功能.而 ...
- Java EE学习记录(一)
话说大家都在说java EE,但是java EE的分层结构如下: 1.数据持久层:主要由一些负责操作POJO(Plain Old Java Object)的类构成,主要负责将数据保存进入数据库; 2. ...
- Scrapy学习-23-分布式爬虫
scrapy-redis分布式爬虫 分布式需要解决的问题 request队列集中管理 去重集中管理 存储管理 使用scrapy-redis实现分布式爬虫 github开源项目: https://g ...
- AutoResetEvent 和 ManualResetEvent 多线程应用
AutoResetEvent 1.用于在多线程,对线程进行阻塞放行 static AutoResetEvent auth0 = new AutoResetEvent(false); static Au ...
- Z划分空间
/* https://blog.csdn.net/fastkeeper/article/details/38905249 https://max.book118.com/html/2017/1007/ ...
- Codeforces 161D Distance in Tree(树型DP)
题目链接 Distance in Tree $k <= 500$ 这个条件十分重要. 设$f[i][j]$为以$i$为子树,所有后代中相对深度为$j$的结点个数. 状态转移的时候,一个结点的信息 ...