示例:二进制序列化委托
来源:互联网 发布:全程软件测试 编辑:程序博客网 时间:2024/06/06 15:33
用途:将委托序列化成二进制,一般用于远程调用方法;
示例:
1、单元测试:
[TestMethod] public void TestSerializableDelegate() { MyAction<string> s = list => { Debug.WriteLine(list); }; s("调用原委托"); string xmls = s.SerializeBinary<MyAction<string>>(); MyAction<string> act = xmls.SerializeDeBinary<MyAction<string>>(); act("反序列化委托调用"); }
2、定义可序列化的委托:
[Serializable] public delegate void MyAction<in T>(T obj);
/3、二进制序列化扩展方法:
public static class BinarySerializeEx { /// <summary> 序列化二进制 支持序列化委托 </summary> public static string SerializeBinary<T>(this object target) { T result = (T)target; // 序列化 using (MemoryStream stream = new MemoryStream()) { BinaryFormatter format = new BinaryFormatter(); format.Serialize(stream, result); return Convert.ToBase64String(stream.ToArray()); //return Encoding.UTF8.GetString(stream.ToArray()); } } /// <summary> 返序列化二进制 支持反学列话委托 </summary> public static T SerializeDeBinary<T>(this string target) { //using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(target))) using (MemoryStream ms = new MemoryStream(Convert.FromBase64String(target))) { BinaryFormatter format = new BinaryFormatter(); return (T)format.Deserialize(ms); } } }
0 0
- 示例:二进制序列化委托
- 关于数据序列化(2)二进制流示例
- 二进制序列化 BinaryFormater
- 二进制序列化
- 委托示例
- 委托示例
- 委托示例
- 对象序列化(二进制序列化)
- 文本文件序列化和二进制序列化
- android 二进制传输 序列化
- C# 对象二进制序列化
- .net对象二进制序列化
- Serialization(序列化) 示例
- XML序列化示例
- XML 序列化示例
- Java序列化示例
- Parcelable序列化示例
- C#: 序列化技术示例
- C++命名空间的使用
- 微信小程序特殊效果合集第一期
- Java网络编程- IO多路复用(多Reactor)(主从式Reactor)
- Java程序实现捕获控制台输出的程序
- linux 软件的安装和卸载
- 示例:二进制序列化委托
- 深度学习和自然语言处理中的attention和memory机制
- 百度编辑器的简单使用
- html中的链接详解
- 魔鬼作坊15款-DNF商业变态辅助的修炼之路
- 题解——Leetcode 2. Add Two Numbers 难度:Medium
- html jquery post后更新整个页面
- Animation
- leetcode题解-39. Combination Sum