コード例 #1
0
        public static XmlAttributeOverrides Create(Type objectType)
        {
            XmlAttributeOverrides xOver = null;

            if (!table.TryGetValue(objectType, out xOver))
            {
                // Create XmlAttributeOverrides object.
                xOver = new XmlAttributeOverrides();

                /* Create an XmlTypeAttribute and change the name of the XML type. */
                XmlTypeAttribute xType = new XmlTypeAttribute();
                xType.TypeName = objectType.Name;

                // Set the XmlTypeAttribute to the XmlType property.
                XmlAttributes attrs = new XmlAttributes();
                attrs.XmlType = xType;

                /* Add the XmlAttributes to the XmlAttributeOverrides,
                 * specifying the member to override. */
                xOver.Add(objectType, attrs);

                table.MergeSafe(objectType, xOver);
            }

            return(xOver);
        }
コード例 #2
0
        /// <summary>
        /// Create or reuse a XmlSerializer with a specific override!
        /// The .NET Factory method "CreateSerializer" do not support caching with a custom override!
        /// This method is optimized for speed and is threadsafe.
        /// </summary>
        /// <param name="factory"></param>
        /// <param name="type"></param>
        /// <param name="overrides"></param>
        /// <returns></returns>
        public static XmlSerializer GetSerializer(this XmlSerializerFactory factory, Type type, XmlAttributeOverrides overrides)
        {
            XmlSerializer result = null;

            string key = type.AssemblyQualifiedName;

            if (overrides != null)
            {
                key += overrides.GetHashCode();

                if (!serializerTable.TryGetValue(key, out result))
                {
                    result = factory.CreateSerializer(type, overrides);
                    serializerTable.MergeSafe(key, result);
                }
            }
            else
            {
                result = factory.CreateSerializer(type);
            }

            return(result);
        }