Пример #1
0
 /// <summary>
 /// Returns true if the LogicalType represents a complex type
 /// (struct or reference type).
 /// </summary>
 /// <remarks>
 /// Specifically a type that is not Nullable<T>, not a logical array,
 /// not a primitive, not an enum, and not a QName.
 /// </remarks>
 private bool isComplexObjectValue(LogicalType type)
 {
     if (!type.IsNullableType &&
         !SerializationHelper.isLogicalArray(type) &&
         !SerializationHelper.IsSerializationPrimitive(type.Type) &&
         !type.Type.GetTypeInfo().IsEnum&&
         type.CustomSerializer != CustomSerializerType.QName &&
         !SerializationHelper.isBuiltInBinary(type))
     {
         return(true);
     }
     return(false);
 }
Пример #2
0
 /// <summary>
 /// Returns true if the LogicalType represents a Nullable<T> type
 /// and the T is a primitive or enum.
 /// </summary>
 private bool isNullableWithStructValue(LogicalType type)
 {
     return(type.IsNullableType &&
            !SerializationHelper.IsSerializationPrimitive(type.NullableValueType.Type) &&
            !type.NullableValueType.Type.GetTypeInfo().IsEnum);
 }
Пример #3
0
        public void Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, string encodingStyle)
        {
            Debug.Assert(null != _logicalType, "Reflection info not initialized before calling Serialize");
            try
            {
                bool        objIsNotNull = (o != null);
                Type        objType      = objIsNotNull ? o.GetType() : null;
                LogicalType type;
                if (true /* AppDomain.CompatVersion >= AppDomain.Orcas */)
                {
                    if (objIsNotNull && !_logicalType.Type.IsAssignableFrom(objType))
                    {
                        throw new InvalidCastException(SR.Format(SR.XmlInvalidCast, objType.FullName, _logicalType.Type.FullName));
                    }
                    type = _logicalType;
                }
                else
                { // Rogue/Whidbey and earlier
                    type = (objIsNotNull && !_logicalType.Type.IsAssignableFrom(objType)) ?
                           findTypeByType(objType, _defaultNamespace) :
                           _logicalType;
                }

                // The soap message formatter considers enums to be non-nullable.
                // The desktop serializes null enums perfectly. So, we need to
                // flip this bit, so that we can serialize null enums as well.
                if (type.Type.GetTypeInfo().IsEnum)
                {
                    type.TypeAccessor.IsNullable = type.RootAccessor.IsNullable = true;
                }

                if (objIsNotNull && !_isEncoded)
                {
                    // Use custom handling if we are serializing a non-null primitive using literal encoding.
                    if (SerializationHelper.IsSerializationPrimitive(type.Type))
                    {
                        XmlSerializationWriter.SerializePrimitive(o, xmlWriter, type, _defaultNamespace, _isEncoded);
                        xmlWriter.Flush();
                        return;
                    }

                    // Use custom handling if we are serializing a non-null enumeration using literal encoding.
                    if (type.Type.GetTypeInfo().IsEnum)
                    {
                        XmlSerializationWriter.SerializeEnumeration(o, xmlWriter, type, _defaultNamespace, _isEncoded);
                        xmlWriter.Flush();
                        return;
                    }
                }

                // Initialize the formatter
                XmlSerializationWriter serialWriter = initXmlSerializationWriter(xmlWriter, encodingStyle);

                // Initialize the namespaces that will be passed to the serialization writer
                if (namespaces == null || namespaces.Count == 0)
                {
                    // Use the default namespaces if the namespaces parameter is null and
                    // the type requires that we use default namespaces
                    namespaces = useDefaultNamespaces(type) ? defaultNamespace : null;
                }
                else if (objIsNotNull && typeof(System.Xml.Schema.XmlSchema).IsAssignableFrom(objType))
                {
                    // We special case the XmlSchema to consider the Xsd Namespace mapping
                    checkXsdNamespaceMapping(ref namespaces, serialWriter);
                }

                // Serialize the object.
                serialWriter.SerializeAsElement(_isEncoded ? type.TypeAccessor : type.RootAccessor, o, namespaces);

                // We only flush the stream after writing the object since the user may want
                // to furthur manipulate the stream once the object is serialized.
                xmlWriter.Flush();
            }
            catch (Exception e)
            {
                if (e is TargetInvocationException)
                {
                    e = e.InnerException;
                }
                throw new InvalidOperationException(SR.XmlGenError, e);
            }
        }