public PersistContext(object root, PersistContextStates context) { this._root = root; this._context = context; this._savecount = 0; this._infoTypeCache = new Hashtable(); }
public PersistContext(object root, PersistContextStates context, int SaveCount) { _root = root; _context = context; _savecount = SaveCount; _infoTypeCache = new Hashtable(); }
/// <summary> /// Serializes a field on a object using PersistableAttribute attributes /// </summary> /// <param name="obj"></param> /// <param name="fi"></param> /// <param name="info"></param> /// <param name="context"></param> private static void SerializeValue(object obj, FieldInfo fi, SerializationInfo info, StreamingContext context) { try { PersistContext persistContext = (context.Context != null && context.Context is PersistContext) ? context.Context as PersistContext : null; PersistContextStates excludeContext = (persistContext != null) ? persistContext.Context : PersistContextStates.ExcludeNormal; object[] attrs = fi.GetCustomAttributes(typeof(PersistableAttribute), false); if (attrs.Length != 0) { var pattr = attrs[0] as PersistableAttribute; if (pattr.Flags == PersistType.None) { return; } if ((pattr.Context & excludeContext) != 0) // exclude context matches { return; } var indicies = new object[] {}; object value = fi.GetValue(obj); try { info.AddValue(fi.Name, value); if (pattr.Version != 0) { info.AddValue(string.Concat(fi.Name, ".Version"), pattr.Version); } } catch (SerializationException) { // discard serialization exceptions as the same member may be added twice } } else { try { info.AddValue(fi.Name, fi.GetValue(obj)); } catch (SerializationException) { // discard serialization exceptions as the same member may be added twice } } } catch { } }
/// <summary> /// Deserializes a field on a object using PersistableAttribute attributes. /// </summary> /// <param name="obj"> /// </param> /// <param name="fi"> /// </param> /// <param name="info"> /// </param> /// <param name="context"> /// </param> private static void DeserializeValue(object obj, FieldInfo fi, SerializationInfo info, StreamingContext context) { try { PersistContext persistContext = (context.Context != null && context.Context is PersistContext) ? context.Context as PersistContext : null; PersistContextStates excludeContext = (persistContext != null) ? persistContext.Context : PersistContextStates.ExcludeNormal; int curVersion = 0; bool canInit = false; object[] attrs = fi.GetCustomAttributes(typeof(PersistableAttribute), false); if (attrs.Length != 0) { var pattr = attrs[0] as PersistableAttribute; if (pattr.Flags == PersistType.None) { return; } if ((pattr.Context & excludeContext) != 0) { // exclude context matches return; } curVersion = pattr.Version; canInit = (pattr.Flags & PersistType.Initializable) != 0; } int version = 0; string name = fi.Name; string ver = string.Concat(fi.Name, ".Version"); object value = null; Type type = null; // Use enum to avoid exceptions SerializationInfoEnumerator siEnum = info.GetEnumerator(); while (siEnum.MoveNext()) { if (siEnum.Name == ver) { version = (int)siEnum.Value; } if (siEnum.Name == name) { value = siEnum.Value; type = siEnum.ObjectType; } } if ((type == null) || ((version > 0) && (curVersion < version))) { if (canInit) { SetDefaultValue(obj, fi, false); } return; } fi.SetValue(obj, value); } catch { } }