/// <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); } 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); } } }
/// <summary> /// Get a single object which represents the persistable members of the given object. /// </summary> /// <param name="obj"> /// </param> /// <returns> /// The System.Object. /// </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)); }