Example #1
0
 /// <summary>
 /// Creates an XML element out of <paramref name="eventInfo"/>.
 /// </summary>
 /// <param name="eventInfo">The property info.</param>
 /// <returns>An XML element or <see langword="null"/> if <paramref name="eventInfo"/> is <see langword="null"/>.</returns>
 static XElement VisitEventInfo(EventInfo eventInfo)
 => eventInfo != null
         ? new XElement(
     XNames.Elements.Property,
     new XAttribute(XNames.Attributes.Type, TypeNameResolver.GetTypeName(eventInfo.DeclaringType)),
     new XAttribute(XNames.Attributes.Name, eventInfo.Name))
         : null;
Example #2
0
        /// <summary>
        /// De-serializes an object constant.
        /// </summary>
        /// <param name="element">The element from which to de-serialize the constant.</param>
        /// <param name="type">The type.</param>
        /// <returns>The de-serialized object constant.</returns>
        /// <exception cref="System.Runtime.Serialization.SerializationException">Expected type attribute.</exception>
        static object DeserializeCustom(
            XElement element,
            Type type)
        {
            if (element == null)
            {
                throw new ArgumentNullException(nameof(element));
            }

            if (element.Elements().FirstOrDefault() == null)
            {
                return(null);
            }

            string typeString;
            var    typeAttribute = element.Attribute(XNames.Attributes.Type);

            if (typeAttribute == null)
            {
                throw new SerializationException("Expected type attribute.");
            }

            typeString = typeAttribute.Value;

            if (typeString.StartsWith(anonymousTypePrefix, StringComparison.Ordinal))
            {
                return(DeserializeAnonymous(element, type));
            }

            var serializer = new DataContractSerializer(TypeNameResolver.GetType(typeString));

            using (var reader = element.Elements().First().CreateReader())
                return(serializer.ReadObject(reader));
        }
Example #3
0
        /// <summary>
        /// Serializes an object using <see cref="DataContractSerializer"/>.
        /// </summary>
        /// <param name="obj">The object to be serialized.</param>
        /// <param name="type">The type of the object.</param>
        /// <param name="parent">The parent element where to serialize the object to.</param>
        static void SerializeCustom(
            object obj,
            Type type,
            XElement parent)
        {
            if (parent == null)
            {
                throw new ArgumentNullException(nameof(parent));
            }

            var custom = new XElement(
                XNames.Elements.Custom,
                new XAttribute(
                    XNames.Attributes.Type, TypeNameResolver.GetTypeName(type)));

            parent.Add(custom);

            if (obj == null)
            {
                return;
            }

            // create a data contract serializer (works with [Serializable] types too)
            var dcSerializer = new DataContractSerializer(
                obj.GetType(),
                Type.EmptyTypes,
                int.MaxValue,
                false,
                false,
                null);

            using (var writer = custom.CreateWriter())
                dcSerializer.WriteObject(writer, obj);
        }
Example #4
0
        /// <summary>
        /// Serializes a nullable value.
        /// </summary>
        /// <param name="nullable">The nullable value to be serialized.</param>
        /// <param name="type">The type of the value.</param>
        /// <param name="parent">The parent element where to add the serialized.</param>
        static void SerializeNullable(
            object nullable,
            Type type,
            XElement parent)
        {
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }
            if (parent == null)
            {
                throw new ArgumentNullException(nameof(parent));
            }
            if (type.GetGenericArguments().Length <= 0)
            {
                throw new ArgumentException("The type must be generic.", nameof(type));
            }

            var typeArgument    = type.GetGenericArguments()[0];
            var nullableElement = new XElement(
                XNames.Elements.Nullable,
                new XAttribute(XNames.Attributes.IsNull, XmlConvert.ToString(nullable == null)),
                nullable == null
                                            ? new XAttribute(XNames.Attributes.Type, TypeNameResolver.GetTypeName(typeArgument))
                                            : null);

            parent.Add(nullableElement);

            if (nullable == null)
            {
                return;
            }

            // get the serializer for the type argument from the table or
            if (_constantSerializers.TryGetValue(typeArgument, out var serializer))
            {
                serializer(nullable, typeArgument, nullableElement);
            }
            else
            {
                var valueElement = new XElement(
                    XNames.Elements.Custom,
                    new XAttribute(
                        XNames.Attributes.Type, TypeNameResolver.GetTypeName(type)));

                nullableElement.Add(valueElement);

                // create a data contract serializer (should work with [Serializable] types too)
                var dcSerializer = new DataContractSerializer(
                    type,
                    Type.EmptyTypes,
                    int.MaxValue,
                    false,
                    false,
                    null);

                using (var writer = valueElement.CreateWriter())
                    dcSerializer.WriteObject(writer, nullable);
            }
        }
Example #5
0
 /// <summary>
 /// Creates an XML element out of <paramref name="propertyInfo"/>.
 /// </summary>
 /// <param name="propertyInfo">The property info.</param>
 /// <returns>An XML element or <see langword="null"/> if <paramref name="propertyInfo"/> is <see langword="null"/>.</returns>
 static XElement VisitPropertyInfo(PropertyInfo propertyInfo)
 => propertyInfo != null
             ? new XElement(
     XNames.Elements.Property,
     new XAttribute(XNames.Attributes.Type, TypeNameResolver.GetTypeName(propertyInfo.DeclaringType)),
     new XAttribute(XNames.Attributes.Name, propertyInfo.Name),
     VisitParameters(propertyInfo.GetIndexParameters()))
             : null;
Example #6
0
        internal static Type GetDataType(XElement element)
        {
            if (element == null)
            {
                throw new ArgumentNullException(nameof(element));
            }

            var type = TypeNameResolver.GetType(element.Name.LocalName);

            if (type == null && element.Name == XNames.Elements.Anonymous)
            {
                type = DataSerialization.GetType(element);

                if (type == null)
                {
                    throw new SerializationException("Expected constant element's type.");
                }
            }

            if (type == typeof(Nullable <>))
            {
                Type valueType = GetType(element);

                if (valueType != null)
                {
                    return(typeof(Nullable <>).MakeGenericType(valueType));
                }

                var valueElement = element.Elements().FirstOrDefault();

                if (valueElement == null)
                {
                    return(null);
                }

                valueType = GetDataType(valueElement);

                if (valueType == null)
                {
                    return(null);
                }

                return(typeof(Nullable <>).MakeGenericType(valueType));
            }

            if (type == typeof(object))
            {
                return(GetCustomConstantType(element));
            }

            if (type == typeof(Enum))
            {
                return(GetEnumConstantType(element));
            }

            return(type);
        }
Example #7
0
        /// <summary>
        /// Gets the type corresponding to a type name written in an XML attribute.
        /// </summary>
        /// <param name="typeAttribute">The type attribute.</param>
        /// <returns>The specified type.</returns>
        internal static Type GetType(XAttribute typeAttribute)
        {
            if (typeAttribute == null)
            {
                return(null);
            }

            return(TypeNameResolver.GetType(typeAttribute.Value));
        }
        /// <summary>
        /// Creates an XML element out of <paramref name="eventInfo"/>.
        /// </summary>
        /// <param name="eventInfo">The property info.</param>
        /// <returns>An XML element or <see langword="null"/> if <paramref name="eventInfo"/> is <see langword="null"/>.</returns>
        static XElement VisitEventInfo(EventInfo eventInfo)
        {
            if (eventInfo == null)
            {
                return(null);
            }

            return(new XElement(
                       XNames.Elements.Property,
                       new XAttribute(XNames.Attributes.Type, TypeNameResolver.GetTypeName(eventInfo.DeclaringType)),
                       new XAttribute(XNames.Attributes.Name, eventInfo.Name)));
        }
        /// <summary>
        /// Creates an XML element out of <paramref name="propertyInfo"/>.
        /// </summary>
        /// <param name="propertyInfo">The property info.</param>
        /// <returns>An XML element or <see langword="null"/> if <paramref name="propertyInfo"/> is <see langword="null"/>.</returns>
        static XElement VisitPropertyInfo(PropertyInfo propertyInfo)
        {
            if (propertyInfo == null)
            {
                return(null);
            }

            return(new XElement(
                       XNames.Elements.Property,
                       new XAttribute(XNames.Attributes.Type, TypeNameResolver.GetTypeName(propertyInfo.DeclaringType)),
                       new XAttribute(XNames.Attributes.Name, propertyInfo.Name),
                       VisitParameters(propertyInfo.GetIndexParameters())));
        }
Example #10
0
        /// <summary>
        /// If the unary expression has type conversion component, this method creates an &quot;asType&quot; attribute.
        /// </summary>
        /// <param name="node">The unary node.</param>
        /// <returns>An &quot;asType&quot; attribute or <see langword="null"/> if <paramref name="node"/> does not contain type conversion component.</returns>
        static XAttribute VisitAsType(UnaryExpression node)
        {
            if (node == null)
            {
                throw new ArgumentNullException(nameof(node));
            }

            if (node.NodeType != ExpressionType.TypeAs && node.NodeType != ExpressionType.Convert)
            {
                return(null);
            }

            return(new XAttribute(XNames.Attributes.Type, TypeNameResolver.GetTypeName(node.Type)));
        }
Example #11
0
        static Type GetEnumConstantType(XElement element)
        {
            if (element == null)
            {
                throw new ArgumentNullException(nameof(element));
            }

            var typeAttribute = element.Attribute(XNames.Attributes.Type);

            if (typeAttribute == null)
            {
                throw new SerializationException("Expected type attribute.");
            }

            return(TypeNameResolver.GetType(typeAttribute.Value));
        }
Example #12
0
        /// <summary>
        /// Creates an XML element out of <paramref name="methodInfo"/>.
        /// </summary>
        /// <param name="methodInfo">The method info.</param>
        /// <returns>An XML element or <see langword="null"/> if <paramref name="methodInfo"/> is <see langword="null"/>.</returns>
        static XElement VisitMethodInfo(MethodInfo methodInfo)
        {
            if (methodInfo == null)
            {
                return(null);
            }

            XAttribute visibility = null;

            if (!methodInfo.IsPublic)
            {
                if (methodInfo.IsPrivate)
                {
                    visibility = new XAttribute(XNames.Attributes.Visibility, XNames.Attributes.Private);
                }
                else
                if (methodInfo.IsAssembly)
                {
                    visibility = new XAttribute(XNames.Attributes.Visibility, XNames.Attributes.Assembly);
                }
                else
                if (methodInfo.IsFamily)
                {
                    visibility = new XAttribute(XNames.Attributes.Visibility, XNames.Attributes.Family);
                }
                else
                if (methodInfo.IsFamilyAndAssembly)
                {
                    visibility = new XAttribute(XNames.Attributes.Visibility, XNames.Attributes.FamilyAndAssembly);
                }
                else
                if (methodInfo.IsFamilyOrAssembly)
                {
                    visibility = new XAttribute(XNames.Attributes.Visibility, XNames.Attributes.FamilyOrAssembly);
                }
            }

            return(new XElement(
                       XNames.Elements.Method,
                       new XAttribute(XNames.Attributes.Type, TypeNameResolver.GetTypeName(methodInfo.DeclaringType)),
                       visibility,
                       methodInfo.IsStatic ? new XAttribute(XNames.Attributes.Static, true) : null,
                       new XAttribute(XNames.Attributes.Name, methodInfo.Name),
                       new XElement(
                           XNames.Elements.Parameters,
                           VisitParameters(methodInfo.GetParameters()))));
        }
Example #13
0
        /// <summary>
        /// Creates an XML element out of <paramref name="constructorInfo"/>.
        /// </summary>
        /// <param name="constructorInfo">The method info.</param>
        /// <returns>An XML element or <see langword="null"/> if <paramref name="constructorInfo"/> is <see langword="null"/>.</returns>
        static XElement VisitConstructorInfo(ConstructorInfo constructorInfo)
        {
            if (constructorInfo == null)
            {
                return(null);
            }

            XAttribute visibility = null;

            if (!constructorInfo.IsPublic)
            {
                if (constructorInfo.IsPrivate)
                {
                    visibility = new XAttribute(XNames.Attributes.Visibility, XNames.Attributes.Private);
                }
                else
                if (constructorInfo.IsAssembly)
                {
                    visibility = new XAttribute(XNames.Attributes.Visibility, XNames.Attributes.Assembly);
                }
                else
                if (constructorInfo.IsFamily)
                {
                    visibility = new XAttribute(XNames.Attributes.Visibility, XNames.Attributes.Family);
                }
                else
                if (constructorInfo.IsFamilyAndAssembly)
                {
                    visibility = new XAttribute(XNames.Attributes.Visibility, XNames.Attributes.FamilyAndAssembly);
                }
                else
                if (constructorInfo.IsFamilyOrAssembly)
                {
                    visibility = new XAttribute(XNames.Attributes.Visibility, XNames.Attributes.FamilyOrAssembly);
                }
            }

            return(new XElement(
                       XNames.Elements.Constructor,
                       new XAttribute(XNames.Attributes.Type, TypeNameResolver.GetTypeName(constructorInfo.DeclaringType)),
                       visibility,
                       constructorInfo.IsStatic ? new XAttribute(XNames.Attributes.Static, true) : null,
                       new XElement(
                           XNames.Elements.Parameters,
                           VisitParameters(constructorInfo.GetParameters()))));
        }
Example #14
0
        /// <summary>
        /// Creates a sequence of XML elements for each of the <paramref name="parameters"/>.
        /// </summary>
        /// <param name="parameters">The parameters.</param>
        /// <returns>A sequence of elements.</returns>
        static IEnumerable <XElement> VisitParameters(IEnumerable <ParameterInfo> parameters)
        {
            if (parameters == null)
            {
                yield break;
            }

            foreach (var param in parameters)
            {
                yield return(new XElement(
                                 XNames.Elements.Parameter,
                                 new XAttribute(XNames.Attributes.Type, TypeNameResolver.GetTypeName(param.ParameterType)),
                                 new XAttribute(XNames.Attributes.Name, param.Name),
                                 param.IsOut || param.ParameterType.IsByRef
                                        ? new XAttribute(XNames.Attributes.IsByRef, true)
                                        : null));
            }
        }
Example #15
0
        /// <summary>
        /// Creates an XML element out of <paramref name="fieldInfo"/>.
        /// </summary>
        /// <param name="fieldInfo">The property info.</param>
        /// <returns>An XML element or <see langword="null"/> if <paramref name="fieldInfo"/> is <see langword="null"/>.</returns>
        static XElement VisitFieldInfo(FieldInfo fieldInfo)
        {
            if (fieldInfo == null)
            {
                return(null);
            }

            XAttribute visibility = null;

            if (!fieldInfo.IsPublic)
            {
                if (fieldInfo.IsAssembly)
                {
                    visibility = new XAttribute(XNames.Attributes.Visibility, XNames.Attributes.Assembly);
                }
                else
                if (fieldInfo.IsFamily)
                {
                    visibility = new XAttribute(XNames.Attributes.Visibility, XNames.Attributes.Family);
                }
                else
                if (fieldInfo.IsPrivate)
                {
                    visibility = new XAttribute(XNames.Attributes.Visibility, XNames.Attributes.Private);
                }
                else
                if (fieldInfo.IsFamilyAndAssembly)
                {
                    visibility = new XAttribute(XNames.Attributes.Visibility, XNames.Attributes.FamilyAndAssembly);
                }
                else
                if (fieldInfo.IsFamilyOrAssembly)
                {
                    visibility = new XAttribute(XNames.Attributes.Visibility, XNames.Attributes.FamilyOrAssembly);
                }
            }

            return(new XElement(
                       XNames.Elements.Field,
                       new XAttribute(XNames.Attributes.Type, TypeNameResolver.GetTypeName(fieldInfo.DeclaringType)),
                       visibility,
                       fieldInfo.IsStatic ? new XAttribute(XNames.Attributes.Static, true) : null,
                       new XAttribute(XNames.Attributes.Name, fieldInfo.Name)));
        }
Example #16
0
        /// <summary>
        /// Adds the type attribute.
        /// </summary>
        /// <param name="element">The element.</param>
        /// <param name="expression">The expression.</param>
        /// <param name="defaultType">The default type.</param>
        /// <returns>XElement.</returns>
        /// <exception cref="System.ArgumentNullException">
        /// element
        /// or
        /// expression
        /// </exception>
        public static XElement AddTypeAttribute(
            this XElement element,
            Expression expression,
            Type defaultType = null)
        {
            if (element == null)
            {
                throw new ArgumentNullException(nameof(element));
            }
            if (expression == null)
            {
                throw new ArgumentNullException(nameof(expression));
            }

            if (expression.Type != defaultType)
            {
                element.Add(
                    new XAttribute(
                        XNames.Attributes.Type,
                        TypeNameResolver.GetTypeName(expression.Type)));
            }

            return(element);
        }