/// <summary>
        /// Initializes a new instance of the <see cref="XmlSerializationContextInfo" /> class.
        /// </summary>
        /// <param name="xmlReader">The XML reader.</param>
        /// <param name="model">The model.</param>
        /// <exception cref="ArgumentNullException">The <paramref name="xmlReader" /> is <c>null</c>.</exception>
        /// <exception cref="ArgumentNullException">The <paramref name="model" /> is <c>null</c>.</exception>
        public XmlSerializationContextInfo(XmlReader xmlReader, ModelBase model)
        {
            Argument.IsNotNull("xmlReader", xmlReader);
            Argument.IsNotNull("model", model);

            var modelType = model.GetType();
            var elementStart = string.Format("<{0}", modelType.Name);

            if (xmlReader.HasAttributes)
            {
                for (int i = 0; i < xmlReader.AttributeCount; i++)
                {
                    xmlReader.MoveToAttribute(i);

                    var attributeName = xmlReader.LocalName;
                    var attributeValue = xmlReader.Value;

                    elementStart += string.Format(" {0}=\"{1}\"", attributeName, attributeValue);
                }

                xmlReader.MoveToElement();
            }

            elementStart += ">";

            xmlReader.MoveToContent();

            var xmlContent = xmlReader.ReadInnerXml();
            if (xmlContent.StartsWith("&lt;"))
            {
#if SL5
                xmlContent = System.Windows.Browser.HttpUtility.HtmlDecode(xmlContent);
#else
                xmlContent = System.Net.WebUtility.HtmlDecode(xmlContent);
#endif
            }

            var elementEnd = string.Format("</{0}>", modelType.Name);

            var finalXmlContent = string.Format("{0}{1}{2}", elementStart, xmlContent, elementEnd);
            Initialize(finalXmlContent, model);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="XmlSerializationContextInfo" /> class.
        /// </summary>
        /// <param name="xmlReader">The XML reader.</param>
        /// <param name="model">The model.</param>
        /// <exception cref="ArgumentNullException">The <paramref name="xmlReader" /> is <c>null</c>.</exception>
        /// <exception cref="ArgumentNullException">The <paramref name="model" /> is <c>null</c>.</exception>
        public XmlSerializationContextInfo(XmlReader xmlReader, ModelBase model)
        {
            Argument.IsNotNull("xmlReader", xmlReader);
            Argument.IsNotNull("model", model);

            xmlReader.MoveToContent();

            var xmlContent = xmlReader.ReadInnerXml();
            if (xmlContent.StartsWith("&lt;"))
            {
#if SL5
                xmlContent = System.Windows.Browser.HttpUtility.HtmlDecode(xmlContent);
#else
                xmlContent = System.Net.WebUtility.HtmlDecode(xmlContent);
#endif
            }

            var modelType = model.GetType();
            var elementStart = string.Format("<{0}>", modelType.Name);
            var elementEnd = string.Format("</{0}>", modelType.Name);

            var finalXmlContent = string.Format("{0}{1}{2}", elementStart, xmlContent, elementEnd);
            Initialize(finalXmlContent, model);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Returns a message that contains all the current warnings.
        /// </summary>
        /// <param name="model">The model base.</param>
        /// <param name="userFriendlyObjectName">Name of the user friendly object.</param>
        /// <returns>
        /// Warning string or empty in case of no warnings.
        /// </returns>
        /// <exception cref="ArgumentNullException">The <paramref name="model"/> is <c>null</c>.</exception>
        public static string GetWarningMessage(this ModelBase model, string userFriendlyObjectName = null)
        {
            Argument.IsNotNull("model", model);

            var validationContext = ((IModelValidation)model).ValidationContext;

            if (!validationContext.HasWarnings)
            {
                return(string.Empty);
            }

            if (string.IsNullOrEmpty(userFriendlyObjectName))
            {
                // Use the real entity name (stupid developer that passes a useless value)
                userFriendlyObjectName = model.GetType().Name;
            }

            var messageBuilder = new StringBuilder();

            messageBuilder.AppendLine(ResourceHelper.GetString("WarningsFound"), userFriendlyObjectName);
            messageBuilder.Append(GetListMessages(validationContext, ValidationResultType.Warning));

            return(messageBuilder.ToString());
        }
        /// <summary>
        /// Returns a message that contains all the current errors.
        /// </summary>
        /// <param name="model">The model base.</param>
        /// <param name="userFriendlyObjectName">Name of the user friendly object.</param>
        /// <returns>
        /// Error string or empty in case of no errors.
        /// </returns>
        /// <exception cref="ArgumentNullException">The <paramref name="model"/> is <c>null</c>.</exception>
        public static string GetErrorMessage(this ModelBase model, string userFriendlyObjectName = null)
        {
            Argument.IsNotNull("model", model);

            var validationContext = ((IModelValidation)model).ValidationContext;

            if (!validationContext.HasErrors)
            {
                return(string.Empty);
            }

            if (string.IsNullOrEmpty(userFriendlyObjectName))
            {
                // Use the real entity name (stupid developer that passes a useless value)
                userFriendlyObjectName = model.GetType().Name;
            }

            var messageBuilder = new StringBuilder();

            messageBuilder.AppendLine($"Found the following errors in '{userFriendlyObjectName}'");
            messageBuilder.Append(GetListMessages(validationContext, ValidationResultType.Error));

            return(messageBuilder.ToString());
        }
Exemplo n.º 5
0
            /// <summary>
            /// Creates a backup of the object property values.
            /// </summary>
            private void CreateBackup()
            {
                using (var stream = new MemoryStream())
                {
                    var propertiesToIgnore = (from propertyData in PropertyDataManager.GetProperties(_object.GetType())
                                              where !propertyData.Value.IncludeInBackup
                                              select propertyData.Value.Name).ToArray();

                    List <PropertyValue> objectsToSerialize;

                    lock (_object._propertyValuesLock)
                    {
                        objectsToSerialize = _object.ConvertDictionaryToListAndExcludeNonSerializableObjects(_object._propertyBag.GetAllProperties(), propertiesToIgnore);
                    }

#if NET
                    var serializer = SerializationHelper.GetBinarySerializer(false);
                    serializer.Serialize(stream, objectsToSerialize);
#else
                    // Xml backup, create serializer without using the cache since the dictionary is used for every object, and
                    // we need a "this" object specific dictionary.
                    var serializer = SerializationHelper.GetDataContractSerializer(GetType(), objectsToSerialize.GetType(),
                                                                                   "backup", objectsToSerialize, false);
                    serializer.WriteObject(stream, objectsToSerialize);

                    _knownTypesForDeserialization = new List <Type>();
                    foreach (var objectToSerialize in objectsToSerialize)
                    {
                        if (objectToSerialize.Value != null)
                        {
                            _knownTypesForDeserialization.Add(objectToSerialize.Value.GetType());
                        }
                    }
#endif

                    _propertyValuesBackup = stream.ToByteArray();
                }

                _objectValuesBackup = new Dictionary <string, object>();
                _objectValuesBackup.Add(IsDirty, _object.IsDirty);
            }