static LoadedAssembly TryReadFromCache(string cacheFileName, DateTime lastWriteTime)
 {
     if (cacheFileName == null || !File.Exists(cacheFileName))
     {
         return(null);
     }
     //LoggingService.Debug("Deserializing " + cacheFileName);
     try {
         using (FileStream fs = new FileStream(cacheFileName, FileMode.Open, FileAccess.Read, FileShare.Read | FileShare.Delete, 4096, FileOptions.SequentialScan)) {
             using (BinaryReader reader = new BinaryReaderWith7BitEncodedInts(fs)) {
                 if (reader.ReadInt64() != lastWriteTime.Ticks)
                 {
                     LoggingService.Debug("Timestamp mismatch, deserialization aborted. (" + cacheFileName + ")");
                     return(null);
                 }
                 FastSerializer s = new FastSerializer();
                 return(s.Deserialize(reader) as LoadedAssembly);
             }
         }
     } catch (IOException ex) {
         LoggingService.Warn(ex);
         return(null);
     } catch (UnauthorizedAccessException ex) {
         LoggingService.Warn(ex);
         return(null);
     } catch (SerializationException ex) {
         LoggingService.Warn(ex);
         return(null);
     }
 }
        public void FixtureSetUp()
        {
            FastSerializer serializer = new FastSerializer();

            using (MemoryStream ms = new MemoryStream()) {
                serializer.Serialize(ms, TypeSystemConvertVisitorTests.ParseTestCase());
                ms.Position = 0;
                testCasePC  = (IProjectContent)serializer.Deserialize(ms);
            }
        }
Exemple #3
0
        public override object Deserialize(Type type, string serializedState, object context)
        {
            var bytes = serializedState.GetBytes();

            using (var stream = new MemoryStream(bytes))
            {
                object result = Serializer.Deserialize(stream, context);
                return(result);
            }
        }
Exemple #4
0
 void Run <T>(T value, Action <T> assert)
 {
     using (var ms = new MemoryStream())
     {
         serializer.Serialize(ms, value);
         ms.Position = 0;
         var copy = serializer.Deserialize <T>(ms);
         assert(copy);
     }
 }
Exemple #5
0
        public void FixtureSetUp()
        {
            CecilLoader loader = new CecilLoader()
            {
                IncludeInternalMembers = true
            };
            IProjectContent pc         = loader.LoadAssemblyFile(typeof(TestCase.SimplePublicClass).Assembly.Location);
            FastSerializer  serializer = new FastSerializer();

            using (MemoryStream ms = new MemoryStream()) {
                serializer.Serialize(ms, pc);
                ms.Position = 0;
                testCasePC  = (IProjectContent)serializer.Deserialize(ms);
            }
        }
        public void FixtureSetUp()
        {
            CecilLoader loader = new CecilLoader()
            {
                IncludeInternalMembers = true
            };
            IUnresolvedAssembly pc         = loader.LoadAssemblyFile(typeof(TestCase.SimplePublicClass).Assembly.Location);
            FastSerializer      serializer = new FastSerializer();

            using (MemoryStream ms = new MemoryStream()) {
                serializer.Serialize(ms, pc);
                ms.Position = 0;
                var asm = (IUnresolvedAssembly)serializer.Deserialize(ms);
                base.compilation = new SimpleCompilation(asm, CecilLoaderTests.Mscorlib);
            }
        }
        static T DeserializeObject <T> (string path) where T : class
        {
            var t = Counters.ParserService.ObjectDeserialized.BeginTiming(path);

            try {
                using (var fs = new FileStream(path, System.IO.FileMode.Open, FileAccess.Read, FileShare.Read, 4096, FileOptions.SequentialScan)) {
                    using (var reader = new BinaryReaderWith7BitEncodedInts(fs)) {
                        lock (sharedSerializer) {
                            return((T)sharedSerializer.Deserialize(reader));
                        }
                    }
                }
            } catch (Exception e) {
                LoggingService.LogError("Error while trying to deserialize " + typeof(T).FullName + ". stack trace:" + Environment.StackTrace, e);
                return(default(T));
            } finally {
                t.Dispose();
            }
        }
Exemple #8
0
        public TestRecord Deserialize(string xmlFilePath)
        {
            string binaryFilePath = GetBinaryFilePath(xmlFilePath);

            // deserialize from the binary format if the file exists
            if (File.Exists(binaryFilePath))
            {
                using (var stream = File.OpenRead(binaryFilePath)) {
                    return((TestRecord)fastSerializer.Deserialize(stream));
                }
            }

            // deserialize from xml if the file exists
            if (File.Exists(xmlFilePath))
            {
                using (var reader = new StreamReader(xmlFilePath)) {
                    return((TestRecord)xmlSerializer.Deserialize(reader));
                }
            }

            return(null);
        }
 static IProjectContent TryReadFromCache(string cacheFileName)
 {
     if (cacheFileName == null || !File.Exists(cacheFileName))
     {
         return(null);
     }
     LoggingService.Debug("Deserializing " + cacheFileName);
     try {
         using (FileStream fs = new FileStream(cacheFileName, FileMode.Open, FileAccess.Read, FileShare.Read | FileShare.Delete, 4096, FileOptions.SequentialScan)) {
             using (BinaryReader reader = new BinaryReaderWith7BitEncodedInts(fs)) {
                 if (reader.ReadInt32() != cacheMagicNumber)
                 {
                     LoggingService.Warn("Incorrect magic number");
                     return(null);
                 }
                 FastSerializer s = new FastSerializer();
                 return((IProjectContent)s.Deserialize(reader));
             }
         }
     } catch (IOException ex) {
         LoggingService.Warn(ex);
         return(null);
     } catch (UnauthorizedAccessException ex) {
         LoggingService.Warn(ex);
         return(null);
     } catch (SerializationException ex) {
         LoggingService.Warn(ex);
         return(null);
     } catch (InvalidCastException ex) {
         // can happen if serialized types are incompatible to expected types
         LoggingService.Warn(ex);
         return(null);
     } catch (FormatException ex) {
         // e.g. invalid 7-bit-encoded int
         LoggingService.Warn(ex);
         return(null);
     }
 }