/// <summary> /// Updates or adds a value with the specified <paramref name="name"/> in the <see cref="SerializationInfo"/>. /// </summary> /// <param name="info">The <see cref="SerializationInfo"/> to be updated.</param> /// <param name="name">The name of the entry to be updated or added.</param> /// <param name="value">The new value to be set.</param> /// <param name="type">The type of the value to be added. This parameter is optional. /// <br/>Default value: <see langword="null"/>.</param> /// <returns><see langword="true"/> if an update occurred (the <see cref="SerializationInfo"/> already contained an entry with the specified <paramref name="name"/>); /// <see langword="false"/> if the value has just been added as a new value.</returns> public static bool UpdateValue(this SerializationInfo info, string name, object value, Type type = null) { if (info == null) { Throw.ArgumentNullException(Argument.info); } if (name == null) { Throw.ArgumentNullException(Argument.name); } if (info.MemberCount == 0 || !info.ContainsName(name)) { // that's why the method is generic: so we can specify a different type by specifying T explicitly info.AddValue(name, value, type ?? value?.GetType() ?? Reflector.ObjectType); return(false); } SerializationInfo clone = InitEmptyInstanceFrom(info); foreach (SerializationEntry entry in info) { if (entry.Name == name) { info.AddValue(name, value, type ?? value?.GetType() ?? Reflector.ObjectType); } else { clone.AddValue(entry.Name, entry.Value, entry.ObjectType); } } SerializationHelper.CopyFields(clone, info); return(true); }
/// <summary> /// Replaces a value with the specified old and new names in the <see cref="SerializationInfo"/>. /// </summary> /// <param name="info">The <see cref="SerializationInfo"/> to be updated.</param> /// <param name="oldName">The name of the entry to be removed.</param> /// <param name="newName">The name of the entry to be added.</param> /// <param name="value">The new value to be set.</param> /// <param name="type">The type of the value to be added. This parameter is optional. /// <br/>Default value: <see langword="null"/>.</param> /// <returns><see langword="true"/> if an entry with the specified old name existed in the <see cref="SerializationInfo"/> /// and the replace has been performed; otherwise, <see langword="false"/>.</returns> public static bool ReplaceValue(this SerializationInfo info, string oldName, string newName, object value, Type type = null) { if (info == null) { Throw.ArgumentNullException(Argument.info); } if (oldName == null) { Throw.ArgumentNullException(Argument.oldName); } if (newName == null) { Throw.ArgumentNullException(Argument.newName); } if (info.MemberCount == 0 || !info.ContainsName(oldName)) { return(false); } SerializationInfo clone = InitEmptyInstanceFrom(info); foreach (SerializationEntry entry in info) { if (entry.Name == oldName) { clone.AddValue(newName, value, type ?? value?.GetType() ?? Reflector.ObjectType); } else { clone.AddValue(entry.Name, entry.Value, entry.ObjectType); } } SerializationHelper.CopyFields(clone, info); return(true); }