private News(SerializationInfo info, StreamingContext ctxt) { int version = info.GetInt16("version"); var fields = this.GetType().GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public).Where(p => p.GetCustomAttribute(typeof(Versioning)) != null); IList <PropertyInfo> props = new List <PropertyInfo>(this.GetType().GetProperties(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public).Where(p => p.GetCustomAttribute(typeof(Versioning)) != null)); var propsAndFields = props.Cast <MemberInfo>().Union(fields.Cast <MemberInfo>()); foreach (SerializationEntry entry in info) { MemberInfo prop = propsAndFields.FirstOrDefault(p => ((Versioning)p.GetCustomAttribute(typeof(Versioning))).Name == entry.Name); if (prop != null) { if (prop is FieldInfo) { ((FieldInfo)prop).SetValue(this, entry.Value); } else { ((PropertyInfo)prop).SetValue(this, entry.Value); } } } var notSetProps = propsAndFields.Where(p => ((Versioning)p.GetCustomAttribute(typeof(Versioning))).Version > version); foreach (MemberInfo notSet in notSetProps) { Versioning ver = (Versioning)notSet.GetCustomAttribute(typeof(Versioning)); if (ver.AutoGenerated) { if (notSet is FieldInfo) { ((FieldInfo)notSet).SetValue(this, ver.DefaultValue); } else { ((PropertyInfo)notSet).SetValue(this, ver.DefaultValue); } } } if (version == 1) { this.IsActionNews = false; } }
public new void GetObjectData(SerializationInfo info, StreamingContext context) { info.AddValue("version", 1); Type myType = this.GetType(); IList <PropertyInfo> props = new List <PropertyInfo>(myType.GetProperties().Where(p => p.GetCustomAttribute(typeof(Versioning)) != null)); foreach (PropertyInfo prop in props) { object propValue = prop.GetValue(this, null); Versioning att = (Versioning)prop.GetCustomAttribute(typeof(Versioning)); info.AddValue(att.Name, propValue); } base.GetObjectData(info, context); }