コード例 #1
0
        protected static MCLSerializer LoadFromFile(string filename, MclSerialization st, Type targettype, string password, bool useCache)
        {
            int           beginTick = Environment.TickCount;
            MCLSerializer ret       = (MCLSerializer)Activator.CreateInstance(targettype);


            if (Settings.NoSave == false && File.Exists(filename))
            {
                MclFile file = MclFile.Open(filename, FileAccess.Read, st, password, useCache);

                int deserializeTick = Environment.TickCount;

                if (file.Content != null)
                {
                    using (MemoryStream ms = new MemoryStream(file.Content))
                    {
                        try
                        {
                            ret = (MCLSerializer) new XmlSerializer(targettype).Deserialize(ms);
                        }
                        catch (InvalidOperationException)
                        {
                            // Deserialize error: XML struct changed, so create a empty mcl serializer.
                            ret = (MCLSerializer)Activator.CreateInstance(targettype);
                        }
                    }
                }

                int deserializeTickConsume = Environment.TickCount - deserializeTick;
                Trace.WriteLineIf(Settings.TraceSwitch.TraceVerbose, "<" + ret.GetType().ToString() + "> Deserialize time (by ticks): " + deserializeTickConsume);
            }

            ret.SerializationType = st;
            ret.FileName          = filename;
            //ret.NSMessageHandler = handler;
            ret.UseCache = useCache;
            ret.password = password;

            int tickConsume = Environment.TickCount - beginTick;

            Trace.WriteLineIf(Settings.TraceSwitch.TraceVerbose, "<" + ret.GetType().ToString() + "> Total loading time (by ticks): " + tickConsume + "\r\n");

            return(ret);
        }
コード例 #2
0
        private void SaveToMCL(string filename)
        {
            int beginTime = Environment.TickCount;

            if (!Settings.NoSave)
            {
                int           serializeBegin = Environment.TickCount;
                XmlSerializer ser            = new XmlSerializer(this.GetType());
                MemoryStream  ms             = new MemoryStream();
                ser.Serialize(ms, this);

                int serializeTickConsume = Environment.TickCount - serializeBegin;
                Trace.WriteLineIf(Settings.TraceSwitch.TraceVerbose, "<" + this.GetType().ToString() + "> serialize time (by ticks): " + serializeTickConsume);

                MclFile file = MclFile.Open(filename, FileAccess.Write, SerializationType, password, UseCache);
                file.Content = ms.ToArray();
                file.Save(filename);
                ms.Close();
            }

            int tickConsume = Environment.TickCount - beginTime;

            Trace.WriteLineIf(Settings.TraceSwitch.TraceVerbose, "<" + this.GetType().ToString() + "> Total saving time (by ticks): " + tickConsume + "\r\n");
        }