コード例 #1
0
        public XmlSerializerHelper()
        {
            this.ns.Add("", "");

            KnownTypesProvider.LoadAllAssemblies = true;
            Type[] knownTypes = KnownTypesProvider.GetKnownTypes("Data", null, new Type[] { typeof(Exception) }).ToArray();
            this.xmlSerializer = new XmlSerializer(typeof(T), knownTypes);
        }
コード例 #2
0
        /// <summary>
        /// Clones an object with the XmlSerializer
        /// </summary>
        /// <typeparam name="T">the object type</typeparam>
        /// <param name="obj">the object</param>
        /// <returns>the clone</returns>
        public static T Clone <T>(this T obj)
        {
            T result = default(T);

            using (Stream memStream = new MemoryStream())
            {
                XmlSerializer serializer = null;
                serializers.TryGetValue(typeof(T), out serializer);
                if (serializer == null)
                {
                    Type[] knownTypes = KnownTypesProvider.GetKnownTypes(null, excludeNameSpacePrefixes: new string[] { }).ToArray();
                    serializer = new XmlSerializer(typeof(T), knownTypes);
                    serializers.Add(typeof(T), serializer);
                }
                serializer.Serialize(memStream, obj);
                memStream.Seek(0, SeekOrigin.Begin);
                result = (T)serializer.Deserialize(memStream);
            }

            return(result);
        }