Пример #1
0
        /// <summary>
        /// Creates a unique signature for the passed
        /// in parameter. MakeKey normalizes array content
        /// and the content of the XmlAttributeOverrides before
        /// creating the key.
        /// </summary>
        /// <param name="type"></param>
        /// <param name="overrides"></param>
        /// <param name="types"></param>
        /// <param name="root"></param>
        /// <param name="defaultNamespace"></param>
        /// <returns></returns>
        public static string Create(Type type, XmlAttributeOverrides overrides, Type[] types, XmlRootAttribute root, string defaultNamespace)
        {
            var keyBuilder = new StringBuilder();

            keyBuilder.Append(type.FullName);
            keyBuilder.Append("??");
            keyBuilder.Append(overrides?.GetHashCode().ToString(CultureInfo.InvariantCulture));
            keyBuilder.Append("??");
            keyBuilder.Append(GetTypeArraySignature(types));
            keyBuilder.Append("??");
            keyBuilder.Append(root?.GetHashCode().ToString(CultureInfo.InvariantCulture));
            keyBuilder.Append("??");
            keyBuilder.Append(defaultNamespace);

            return(keyBuilder.ToString());
        }
Пример #2
0
        public static XmlSerializer GetXmlSerializer(Type objType, XmlAttributeOverrides xmlOverrides)
        {
            string hashkey = string.Empty;

            if (xmlOverrides != null)
            {
                hashkey = xmlOverrides.GetHashCode().ToString();
            }
            string key = "XML" + objType.FullName + hashkey;

            XmlSerializer serializer = (XmlSerializer)GetSerializerObject(key);

            if (serializer == null)
            {
                serializer = factory.CreateSerializer(objType, xmlOverrides);
                SetSerializerObject(key, serializer);
            }
            return(serializer);
        }
        /// <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);
        }