Beispiel #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;
Beispiel #2
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);
        }
Beispiel #3
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);
            }
        }
Beispiel #4
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;
        /// <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())));
        }
Beispiel #7
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)));
        }
Beispiel #8
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()))));
        }
Beispiel #9
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()))));
        }
Beispiel #10
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));
            }
        }
Beispiel #11
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)));
        }
Beispiel #12
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);
        }