C# 实现克隆的工具类型
来源:互联网 发布:金蝶数据库在哪里 编辑:程序博客网 时间:2024/06/04 18:21
实现克隆的工具类型要求类型必须是可序列化的
public enum FormatterType
{
/// <summary>
/// SOAP消息格式编码
/// </summary>
Soap,
/// <summary>
/// 二进制消息格式编码
/// </summary>
Binary
}
public static class SerializationHelper
{
private const FormatterType DefaultFormatterType = FormatterType.Binary;
/// <summary>
/// 按照串行化的编码要求,生成对应的编码器。
/// </summary>
/// <param name="formatterType"></param>
/// <returns></returns>
private static IRemotingFormatter GetFormatter(FormatterType formatterType)
{
switch (formatterType)
{
case FormatterType.Binary: return new BinaryFormatter();
case FormatterType.Soap: return new SoapFormatter();
}
throw new NotSupportedException();
}
/// <summary>
/// 把对象序列化转换为字符串
/// </summary>
/// <param name="graph">可串行化对象实例</param>
/// <param name="formatterType">消息格式编码类型(Soap或Binary型)</param>
/// <returns>串行化转化结果</returns>
/// <remarks>调用BinaryFormatter或SoapFormatter的Serialize方法实现主要转换过程。
/// </remarks>
public static string SerializeObjectToString(object graph, FormatterType formatterType)
{
using (MemoryStream memoryStream = new MemoryStream())
{
IRemotingFormatter formatter = GetFormatter(formatterType);
formatter.Serialize(memoryStream, graph);
Byte[] arrGraph = memoryStream.ToArray();
return Convert.ToBase64String(arrGraph);
}
}
public static string SerializeObjectToString(object graph)
{
return SerializeObjectToString(graph, DefaultFormatterType);
}
/// <summary>
/// 把已序列化为字符串类型的对象反序列化为指定的类型
/// </summary>
/// <param name="serializedGraph">已序列化为字符串类型的对象</param>
/// <param name="formatterType">消息格式编码类型(Soap或Binary型)</param>
/// <typeparam name="T">对象转换后的类型</typeparam>
/// <returns>串行化转化结果</returns>
/// <remarks>调用BinaryFormatter或SoapFormatter的Deserialize方法实现主要转换过程。
/// </remarks>
public static T DeserializeStringToObject<T>(string graph, FormatterType formatterType)
{
Byte[] arrGraph = Convert.FromBase64String(graph);
using (MemoryStream memoryStream = new MemoryStream(arrGraph))
{
IRemotingFormatter formatter = GetFormatter(formatterType);
return (T)formatter.Deserialize(memoryStream);
}
}
public static T DeserializeStringToObject<T>(string graph)
{
return DeserializeStringToObject<T>(graph, DefaultFormatterType);
}
}
public static class Test
{
public static T Clone<T>(this T t) where T : class
{
Type target = t.GetType();
object[] attributes = target.GetCustomAttributes(typeof(SerializableAttribute), false);
if (attributes.Length == 0)
{
//throw new ArgumentException("T must be Serializabled");采用json序列化
//需要引用Newtonsoft.Json.dll
Newtonsoft.Json.JsonSerializer ser = new Newtonsoft.Json.JsonSerializer();
StringWriter sw = new StringWriter();
Newtonsoft.Json.JsonTextWriter writer = new Newtonsoft.Json.JsonTextWriter(sw);
ser.Serialize(writer, t);
StringReader sr = new StringReader(sw.ToString());
Newtonsoft.Json.JsonTextReader reader = new Newtonsoft.Json.JsonTextReader(sr);
return ser.Deserialize<T>(reader);
}
else
{
string graph = SerializationHelper.SerializeObjectToString(t);
return SerializationHelper.DeserializeStringToObject<T>(graph);
}
}
}
需要引用System.Runtime.Serialization.Formatters.Soap.dll
- C# 实现克隆的工具类型
- C# 实现可克隆(ICloneable)的类型
- C#中完美克隆引用类型的对象
- 如何简单地实现引用类型的深度克隆
- C#中实现任意类的完美克隆
- 实现对象的克隆
- C#对象克隆的战争
- c#中对象的克隆
- C#对象克隆的战争
- java 实现对象的克隆
- c# 克隆
- 浅克隆与深克隆的区别以及实现
- Java深克隆和浅克隆的原理及实现
- JAVA里的深克隆与浅克隆实现
- 一个克隆对象的C#基类
- 一个克隆对象的C#基类
- C# 工具类--类型转换的泛型方法
- C#实现的知识搜索工具 - KBCruiser
- 通用的自定义上下文对象
- 根据类型名称生成类型实例
- ORACLE ORDER BY用法总结
- C#教学第23讲类的构造2(学习笔记)
- 飞屋环游记
- C# 实现克隆的工具类型
- 机子出错蓝屏,求高手说一下怎么解决!!
- Tourism
- VMWare三种工作模式
- ubuntu 安装飞鸽
- 访问修饰符
- js 代码
- 转:经典Excel VBA代码
- 用两种不同的方法 计算出字符串的长度 11月15日