/// <summary>
/// 支持序列化的对象基类
/// </summary>
[Serializable]
public class LibSerializationObject : ISerializable, IDeserializationCallback
{
#region 构造函数
/// <summary>
/// 构造函数
/// </summary>
public LibSerializationObject()
{
}
#endregion
#region 序列化相关方法成员
/// <summary>
/// 构造函数
/// </summary>
/// <param name="info"></param>
/// <param name="context"></param>
public LibSerializationObject(SerializationInfo info, StreamingContext context)
{
}
/// <summary>
///
/// </summary>
/// <param name="info"></param>
/// <param name="context"></param>
public virtual void GetObjectData(SerializationInfo info, StreamingContext context)
{
}
#endregion
#region 序列化值获取相关方法
/// <summary>
/// 获取SerializationInfo中的值
/// </summary>
/// <param name="info">SerializationInfo对象</param>
/// <param name="propertyName">值名称</param>
/// <param name="type">值类型</param>
/// <param name="defaultValue">默认值</param>
/// <returns>返回对应值</returns>
public object GetObjectValue(SerializationInfo info, string propertyName, Type type, object defaultValue)
{
object objResult = defaultValue;
PropertyInfo propertyInfo = info.GetType().GetProperty("MemberNames", BindingFlags.NonPublic | BindingFlags.Instance);
if (propertyInfo != null)
{
string[] propertyNames = (string[])propertyInfo.GetValue(info, null);// info.MemberNames;
if(LibSysUtils.ArrayIncludeSTR(propertyNames, propertyName))
{
objResult = info.GetValue(propertyName, type);
if(objResult == null)
{
objResult = defaultValue;
}
}
}
return objResult;
}
/// <summary>
/// 获取SerializationInfo中的值类型为bool的值
/// </summary>
/// <param name="info">SerializationInfo对象</param>
/// <param name="propertyName">值名称</param>
/// <param name="defaultValue">默认值</param>
/// <returns>返回对应值</returns>
public bool GetBoolValue(SerializationInfo info, string propertyName, bool defaultValue)
{
object objResult = this.GetObjectValue(info, propertyName, typeof(bool), defaultValue);
return LibSysUtils.ToBoolean(objResult);
}
/// <summary>
/// 获取SerializationInfo中的值类型为Int的值
/// </summary>
/// <param name="info">SerializationInfo对象</param>
/// <param name="propertyName">值名称</param>
/// <param name="defaultValue">默认值</param>
/// <returns>返回对应值</returns>
public int GetIntValue(SerializationInfo info, string propertyName, int defaultValue)
{
object objResult = this.GetObjectValue(info, propertyName, typeof(int), defaultValue);
return LibSysUtils.ToInteger(objResult);
}
/// <summary>
/// 获取SerializationInfo中的值类型为long的值
/// </summary>
/// <param name="info">SerializationInfo对象</param>
/// <param name="propertyName">值名称</param>
/// <param name="defaultValue">默认值</param>
/// <returns>返回对应值</returns>
public long GetLongValue(SerializationInfo info, string propertyName, int defaultValue)
{
object objResult = this.GetObjectValue(info, propertyName, typeof(int), defaultValue);
return LibSysUtils.ToLong(objResult);
}
/// <summary>
/// 获取SerializationInfo中的值类型为String的值
/// </summary>
/// <param name="info">SerializationInfo对象</param>
/// <param name="propertyName">值名称</param>
/// <param name="defaultValue">默认值</param>
/// <returns>返回对应值</returns>
public string GetStringValue(SerializationInfo info, string propertyName, string defaultValue)
{
object objResult = this.GetObjectValue(info, propertyName, typeof(string), defaultValue);
return LibSysUtils.ToString(objResult);
}
/// <summary>
/// 获取SerializationInfo中的值类型为DateTime的值
/// </summary>
/// <param name="info">SerializationInfo对象</param>
/// <param name="propertyName">值名称</param>
/// <param name="defaultValue">默认值</param>
/// <returns>返回对应值</returns>
public DateTime GetDateTimeValue(SerializationInfo info, string propertyName, DateTime defaultValue)
{
object objResult = this.GetObjectValue(info, propertyName, typeof(DateTime), defaultValue);
return LibSysUtils.ToDateTime(objResult);
}
/// <summary>
/// 获取SerializationInfo中的值类型为Double的值
/// </summary>
/// <param name="info">SerializationInfo对象</param>
/// <param name="propertyName">值名称</param>
/// <param name="defaultValue">默认值</param>
/// <returns>返回对应值</returns>
public double GetDoubleValue(SerializationInfo info, string propertyName, double defaultValue)
{
object objResult = this.GetObjectValue(info, propertyName, typeof(double), defaultValue);
return LibSysUtils.ToDouble(objResult);
}
/// <summary>
/// 获取SerializationInfo中的值类型为Decimal的值
/// </summary>
/// <param name="info">SerializationInfo对象</param>
/// <param name="propertyName">值名称</param>
/// <param name="defaultValue">默认值</param>
/// <returns>返回对应值</returns>
public decimal GetDecimalValue(SerializationInfo info, string propertyName, decimal defaultValue)
{
object objResult = this.GetObjectValue(info, propertyName, typeof(decimal), defaultValue);
return LibSysUtils.ToDecimal(objResult);
}
/// <summary>
/// 获取SerializationInfo中的值类型为Color的值
/// </summary>
/// <param name="info">SerializationInfo对象</param>
/// <param name="propertyName">值名称</param>
/// <param name="defaultValue">默认值</param>
/// <returns>返回对应值</returns>
public Color GetColorValue(SerializationInfo info, string propertyName, Color defaultValue)
{
object objResult = this.GetObjectValue(info, propertyName, typeof(Color), defaultValue);
return (Color)objResult;
}
#endregion
#region 序列化回调处理
protected virtual void AfterDeserialization(object sender)
{
}
#endregion
public void OnDeserialization(object sender)
{
this.AfterDeserialization(sender);
}
#region 拷贝方法
/// <summary>克隆自身</summary>
/// <returns></returns>
public object CloneSelf()
{
object result = LibSerializerControl.CloneObject(this);
return result;
}
#endregion
}
【代码片段】序列化和反序列化之对象基类
最新推荐文章于 2021-03-05 09:51:28 发布