usingSystem;usingSystem.Collections.Generic;usingSystem.IO;usingSystem.Linq;usingSystem.Runtime.Serialization;usingSystem.Runtime.Serialization.Formatters.Binary;usingSystem.Text;usingSystem.Threading.Tasks;usingNewtonsoft.Json;usingSystem.Xml.Serialization;namespaceTest_Control
{classProgram
{static void Main(string[] args)
{
Object student= new Student() { StudentID = "99", StudentName = "zhang"};string result = ObjectToString(student);
Console.WriteLine(result+ "\r\n");
Student newResult= StringToObject(result);
Console.WriteLine("ID:{0}, Name:{1}" + "\r\n", newResult.StudentID, newResult.StudentName);//使用UTF8编码将字符串转成byte数组
var byt = SerializeHelper.ConvertToByte("12zhang3");//Console.WriteLine(BitConverter.ToString(byt));//使用UTF8编码将byte数组转成字符串
string str =SerializeHelper.ConvertToString(byt);
Console.WriteLine(str);//使用指定字符编码将byte数组转成字符串
string strr =SerializeHelper.ConvertToString(byt, Encoding.Unicode);
Console.WriteLine(strr);//将对象序列化为二进制数据
var obj = new Student() { StudentID = "9", StudentName = "zhang"};var arr =SerializeHelper.SerializeToBinary(obj);
Console.WriteLine(BitConverter.ToString(arr));//将对象序列化为XML数据 并且打印
var xml =SerializeHelper.SerializeToXml(obj);
Console.WriteLine(BitConverter.ToString(xml));//将二进制数据反序列化 并且打印
var Bt = new byte[] { 12, 12, 24};
Student obje= SerializeHelper.DeserializeWithBinary(arr);
Console.WriteLine(obje.StudentName);
Console.ReadKey();
}///
///对象转字符串(序列化后转Base64编码字符串)///
/// 对象
/// 字符串
public static string ObjectToString(T obj)
{using (MemoryStream stream = newMemoryStream())
{
IFormatter formatter= newBinaryFormatter();
formatter.Serialize(stream, obj);
stream.Position= 0;byte[] buffer = new byte[stream.Length];
stream.Read(buffer,0, buffer.Length);returnConvert.ToBase64String(buffer);
}
}///
///字符串转对象(Base64编码字符串反序列化为对象)///
/// 字符串
/// 对象
public static T StringToObject(stringstr)
{using (MemoryStream stream = newMemoryStream())
{byte[] bytes =Convert.FromBase64String(str);
stream.Write(bytes,0, bytes.Length);
stream.Position= 0;
IFormatter formatter= newBinaryFormatter();return(T)formatter.Deserialize(stream);
}
}
}///
///可序列化的类,用Serializable标示此类可序列化///
[Serializable]public classStudent
{public string StudentID { get; set; }public string StudentName { get; set; }
}public static classSerializeHelper
{///
///使用UTF8编码将byte数组转成字符串///
///
///
public static string ConvertToString(byte[] data)
{return Encoding.UTF8.GetString(data, 0, data.Length);
}///
///使用指定字符编码将byte数组转成字符串///
///
///
///
public static string ConvertToString(byte[] data, Encoding encoding)
{return encoding.GetString(data, 0, data.Length);
}///
///使用UTF8编码将字符串转成byte数组///
///
///
public static byte[] ConvertToByte(stringstr)
{returnEncoding.UTF8.GetBytes(str);
}///
///使用指定字符编码将字符串转成byte数组///
///
///
///
public static byte[] ConvertToByte(stringstr, Encoding encoding)
{returnencoding.GetBytes(str);
}///
///将对象序列化为二进制数据///
///
///
public static byte[] SerializeToBinary(objectobj)
{
MemoryStream stream= newMemoryStream();
BinaryFormatter bf= newBinaryFormatter();
bf.Serialize(stream, obj);byte[] data =stream.ToArray();
stream.Close();returndata;
}///
///将对象序列化为XML数据///
///
///
public static byte[] SerializeToXml(objectobj)
{
MemoryStream stream= newMemoryStream();
XmlSerializer xs= newXmlSerializer(obj.GetType());
xs.Serialize(stream, obj);byte[] data =stream.ToArray();
stream.Close();returndata;
}///
///将二进制数据反序列化///
///
///
public static object DeserializeWithBinary(byte[] data)
{//注意,在反序列化的时候,将byte[]写入流以后,需要将游标移到首位,即将Position置零,否则反序列化将报错。
MemoryStream stream = newMemoryStream();
stream.Write(data,0, data.Length);
stream.Position= 0;
BinaryFormatter bf= newBinaryFormatter();object obj =bf.Deserialize(stream);
stream.Close();returnobj;
}///
///将二进制数据反序列化为指定类型对象///
///
///
///
public static T DeserializeWithBinary(byte[] data)
{return(T)DeserializeWithBinary(data);
}///
///将XML数据反序列化为指定类型对象///
///
///
///
public static T DeserializeWithXml(byte[] data)
{
MemoryStream stream= newMemoryStream();
stream.Write(data,0, data.Length);
stream.Position= 0;
XmlSerializer xs= new XmlSerializer(typeof(T));object obj =xs.Deserialize(stream);
stream.Close();return(T)obj;
}
}
}