public void SerializeErrors() { ValidationErrors errors = new ValidationErrors(); ErrorMessageOne = new ErrorMessage("Kissing Leads To Brain Disease", new object[] {"Param11", 5, "Param13"}); ErrorMessageTwo = new ErrorMessage("This Is Eva Green", new object[] { "Param21", 'g' , new object[] {"Goran", "Milosavljevic"} }); ErrorMessage ErrorMessageThree = new ErrorMessage("Third error message", null); ErrorMessage ErrorMessageFour = new ErrorMessage("Fourth error message", new object[]{}); errors.AddError("key1", ErrorMessageOne); errors.AddError("key1", ErrorMessageTwo); errors.AddError("key2", ErrorMessageThree); errors.AddError("key3", ErrorMessageFour); Stream streamBefore = new MemoryStream(); Stream streamAfter = new MemoryStream(); // serialize ValidationErrors XmlSerializer serializer = new XmlSerializer(typeof(ValidationErrors)); serializer.Serialize(streamBefore, errors); streamBefore.Position = 0; // deserialize ValidationErrors ValidationErrors result = (ValidationErrors) serializer.Deserialize(streamBefore); // serialize ValidationErrors serializer.Serialize(streamAfter, result); // compare ValidationErrors instances byte[] byteBefore = new byte[streamBefore.Length]; byte[] byteAfter = new byte[streamAfter.Length]; Assert.AreEqual(byteAfter.Length, byteBefore.Length); streamBefore.Position = 0; streamAfter.Position = 0; streamBefore.Read(byteBefore, 0, (int) streamBefore.Length); streamAfter.Read(byteAfter, 0, (int) streamAfter.Length); Assert.AreEqual(byteBefore, byteAfter); }
/// <summary> /// Adds the supplied <paramref name="message"/> to this /// instance's collection of errors. /// </summary> /// <param name="provider"> /// The provider that should be used for message grouping; can't be /// <see lang="null"/>. /// </param> /// <param name="message">The error message to add.</param> /// <exception cref="System.ArgumentNullException"> /// If the supplied <paramref name="provider"/> or <paramref name="message"/> is <see langword="null"/>. /// </exception> public void AddError(string provider, ErrorMessage message) { AssertUtils.ArgumentNotNull(provider, "provider"); AssertUtils.ArgumentNotNull(message, "errorMessage"); List<ErrorMessage> errors; if (!errorMap.TryGetValue(provider, out errors)) { errors = new List<ErrorMessage>(); errorMap[provider] = errors; } errors.Add(message); }
/// <summary> /// Initializes a new instance of the <see cref="ErrorMessage"/> class copying values from another instance. /// </summary> /// <param name="other">Another Error message instance to copy values from.</param> protected ErrorMessage(ErrorMessage other) { this.id = other.id; this.parameters = other.parameters; }
/// <summary> /// Adds the supplied <paramref name="message"/> to this /// instance's collection of errors. /// </summary> /// <param name="provider"> /// The provider that should be used for message grouping; can't be /// <see lang="null"/>. /// </param> /// <param name="message">The error message to add.</param> /// <exception cref="System.ArgumentNullException"> /// If the supplied <paramref name="provider"/> or <paramref name="message"/> is <see langword="null"/>. /// </exception> public void AddError(string provider, ErrorMessage message) { AssertUtils.ArgumentNotNull(provider, "provider"); AssertUtils.ArgumentNotNull(message, "errorMessage"); IList errors = (IList) errorMap[provider]; if (errors == null) { errors = new ArrayList(); errorMap[provider] = errors; } errors.Add(message); }