Exemplo n.º 1
0
        public void IncludeType(Type type)
        {
            System.Diagnostics.Debug.Assert(null != _serializationReflector, "The XmlSerializationReflector has not been initialized.");

            _serializationReflector.FindType(type, false /*Encoded*/, _defaultNs);
            _serializationReflector.ReflectIncludedTypes();
        }
Exemplo n.º 2
0
        /// <summary>
        /// Constructs the XmlSerializer, reflecting over the given
        /// types and any types that are statically referenced from them.
        /// Reflection overrides may be given.
        /// </summary>
        /// <param name="extraTypes">The additional types the serializer should be
        /// prepared to encounter during serialization of the primary <paramref name="type"/>.</param>
        /// <param name="defaultNamespace">The default namespace to use for all the XML elements.</param>
        public XmlSerializer(Type type, XmlAttributeOverrides overrides, Type[] extraTypes, XmlRootAttribute root, string defaultNamespace)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }
            if (defaultNamespace == null)
            {
                defaultNamespace = "";
            }

            _events.sender = this;
            _isEncoded     = false;
            if (root != null)
            {
                if (overrides == null)
                {
                    overrides = new XmlAttributeOverrides();
                }
                // If we're dealing with a nullable type, we need to set the override
                // on the generic type argument as well.
                System.Collections.Generic.List <Type> typesToOverride = new System.Collections.Generic.List <Type>(2);
                typesToOverride.Add(type);
                if (type.GetTypeInfo().IsGenericType&& type.GetGenericTypeDefinition() == typeof(Nullable <>))
                {
                    Type[] args = type.GetGenericArguments();
                    if (args.Length > 0)
                    { // just in case they passed in typeof(Nullable<>) with no generic arg
                        typesToOverride.Add(args[0]);
                    }
                }
                foreach (Type t in typesToOverride)
                {
                    XmlAttributes attrs = overrides[t];
                    if (attrs != null)
                    {
                        attrs.XmlRoot = root;
                    }
                    else
                    {
                        attrs         = new XmlAttributes();
                        attrs.XmlRoot = root;
                        // Preserve any XmlType that may be declared on the type itself,
                        // since by providing this XmlRoot override, we prevent any declared
                        // XmlTypeAttribute from being reflected.
                        object[] declaredAttributes = t.GetTypeInfo().GetCustomAttributes(typeof(XmlTypeAttribute), false).ToArray();
                        if (declaredAttributes.Length > 0)
                        {
                            // The user shouldn't have more than one defined, but just in case he does,
                            // we read the last reflected one, to emulate the behavior of the
                            // TypeAttributes class.
                            attrs.XmlType = (XmlTypeAttribute)declaredAttributes[declaredAttributes.Length - 1];
                        }
                        overrides.Add(t, attrs);
                    }
                }
            }

            _defaultNamespace = defaultNamespace;
            _reflector        = new XmlSerializationReflector(overrides, defaultNamespace);
            // Reflect over the main type.  Never search over intrinsics so we get a
            // LogicalType with a RootAccessor whose Namespace property reflects the defaultNamespace.
            // In fact, any type's RootAccessor.Namespace may not be set correctly given
            // the defaultNamespace if it was reflected over previously.  But since this
            // is the very first request we make of the m_Reflector, we're going to get
            // the right one.  And this is the only place where that is important.
            _logicalType = _reflector.FindTypeForRoot(type, _isEncoded, defaultNamespace);
            // Reflect over the extra types
            if (extraTypes != null && extraTypes.Length > 0)
            {
                _extraTypes = new LogicalType[extraTypes.Length];
                for (int typeNDX = 0; typeNDX < extraTypes.Length; ++typeNDX)
                {
                    _extraTypes[typeNDX] = findTypeByType(extraTypes[typeNDX], defaultNamespace);
                }
            }
            // Reflect over the included types
            _reflector.ReflectIncludedTypes();
            if (true /* AppDomain.CompatVersion >= AppDomain.Orcas */)
            {
                _reflector.ReflectionDisabled = true;
            }
        }