/// <summary> /// Initialize a class based on PersistableAttribute settings. /// </summary> /// <param name="obj">Class instance that is to be initialized.</param> /// <param name="force">Whether to force initialization regardless of the PersistableAttribute</param> public static void Initialize(object obj, bool force) { Type type = obj.GetType(); PersistableAttribute pattr = GetPersistableAttribute(type); if (pattr == null || pattr.Flags == PersistType.None) { return; } if ((pattr.Flags & PersistType.DeclaredOnly | PersistType.Properties) != 0) { var indicies = new object[] {}; foreach (PropertyInfo pi in GetProperties(type, pattr.Flags)) { SetDefaultValue(obj, pi, false); } } if ((pattr.Flags & PersistType.DeclaredOnly | PersistType.Fields) != 0) { foreach (FieldInfo fi in GetFields(type, pattr.Flags)) { SetDefaultValue(obj, fi, false); } } }
/// <summary> /// Deserializes an object using PersistableAttribute attributes /// </summary> /// <param name="obj"></param> /// <param name="info"></param> /// <param name="context"></param> public static void Deserialize(object obj, SerializationInfo info, StreamingContext context) { Type type = obj.GetType(); PersistableAttribute pattr = GetPersistableAttribute(type); PersistType persistType = (pattr == null) ? PersistType.ClassDefault : pattr.Flags; foreach (PropertyInfo pi in GetProperties(type, persistType)) { DeserializeValue(obj, pi, info, context); } foreach (FieldInfo fi in GetFields(type, persistType)) { DeserializeValue(obj, fi, info, context); } #region Queue PostSerialization Callbacks if (typeof(IPostSerializationCallback).IsInstanceOfType(obj)) { PersistContext persistContext = (context.Context != null && context.Context is PersistContext) ? context.Context as PersistContext : null; if (persistContext != null) { persistContext.AddPostSerializationCallback((IPostSerializationCallback)obj); } } #endregion }
/// <summary> /// Get a single object which represents the persistable members of the given object. /// </summary> /// <param name="obj"></param> /// <returns></returns> /// <remarks>Basically for serialize members without needing ISerialize interface.</remarks> public static object GetSerializationSurrogate(object obj) { Type type = obj.GetType(); PersistableAttribute pattr = GetPersistableAttribute(type); PersistType persistType = (pattr == null) ? PersistType.ClassDefault : pattr.Flags; return(GetSerializationSurrogate(obj, persistType)); }
/// <summary> /// Serializes an object using PersistableAttribute attributes /// </summary> /// <param name="obj"></param> /// <param name="info"></param> /// <param name="context"></param> public static void Serialize(object obj, SerializationInfo info, StreamingContext context) { PersistContext persistContext = (context.Context != null && context.Context is PersistContext) ? context.Context as PersistContext : null; if (persistContext != null) { if (persistContext.RetrieveCacheInfo(obj, info)) { return; } } Type type = obj.GetType(); PersistableAttribute pattr = GetPersistableAttribute(type); PersistType persistType = (pattr == null) ? PersistType.ClassDefault : pattr.Flags; if (pattr != null && pattr.Version != 0) { info.AddValue("PersistVersion", pattr.Version); } foreach (PropertyInfo pi in GetProperties(type, persistType)) { SerializeValue(obj, pi, info, context); } foreach (FieldInfo fi in GetFields(type, persistType)) { SerializeValue(obj, fi, info, context); } if (persistContext != null) { persistContext.StoreCacheInfo(obj, info); } }
/// <summary> /// Get a single object which represents the persistable members of the given object. /// </summary> /// <param name="obj"></param> /// <returns></returns> /// <remarks>Basically for serialize members without needing ISerialize interface.</remarks> public static void DeserializationFromSurrogate(object obj, object values) { if (obj == null) { throw new ArgumentNullException("obj"); } var dict = values as ListDictionary; if (dict == null) { throw new ArgumentException("Values should be a Dictionary", "values"); } Type type = obj.GetType(); PersistableAttribute pattr = GetPersistableAttribute(type); if (pattr == null || pattr.Flags == PersistType.None) { return; } DeserializationFromSurrogate(obj, values, pattr.Flags); }