111,094
社区成员




[Serializable]
public class CDetail
{
public double Amout
{
get { return _amout; }
set { _amout = value; }
}
public string Type
{
get { return _type; }
set { _type = value; }
}
public string OtherType
{
get { return _otherType; }
set { _otherType = value; }
}
public string Remark
{
get { return _remark; }
set { _remark = value; }
}
private double _amout;
private string _type;
private string _otherType;
private string _remark;
}
[Serializable]
public class CDetails
{
/// <summary>
/// 1为CEO,2为董事长
/// </summary>
[DefaultValue(1)]
public int CEOOrChairMan;
private List<CDetail> _listDetail = new List<CDetail>();
public List<CDetail> listDetail
{
get { return _listDetail; }
set { _listDetail = value; }
}
public void Add(CDetail c)
{
listDetail.Add(c);
}
}
/// <summary>
/// 序列化 对象到字符串
/// </summary>
/// <param name="obj">泛型对象</param>
/// <returns>序列化后的字符串</returns>
public static string Serialize<T>(T obj)
{
try
{
BinaryFormatter formatter = new BinaryFormatter();
MemoryStream stream = new MemoryStream();
formatter.Serialize(stream, obj);
stream.Position = 0;
byte[] buffer = new byte[stream.Length];
stream.Read(buffer, 0, buffer.Length);
stream.Flush();
stream.Close();
return System.Convert.ToBase64String(buffer);
}
catch (Exception ex)
{
throw new Exception("序列化失败,原因:" + ex.Message);
}
}
/// <summary>
/// 反序列化 字符串到对象
/// </summary>
/// <param name="obj">泛型对象</param>
/// <param name="str">要转换为对象的字符串</param>
/// <returns>反序列化出来的对象</returns>
public static T Desrialize<T>(T obj, string str)
{
try
{
//obj = default(T);
BinaryFormatter formatter = new BinaryFormatter();
//formatter.Binder = new BindChanger();
byte[] buffer =System.Convert.FromBase64String(str);
MemoryStream stream = new MemoryStream();
stream.Write(buffer,0,buffer.Length);
stream.Position = 0;
obj = (T)formatter.Deserialize(stream);
stream.Flush();
stream.Close();
}
catch (Exception ex)
{
throw new Exception("反序列化失败,原因:" + ex.Message);
}
return obj;
}