static void SerializeObject(string path, object obj) { if (obj == null) { throw new ArgumentNullException(nameof(obj)); } var t = Counters.ParserService.ObjectSerialized.BeginTiming(path); try { using (var fs = new FileStream(path, System.IO.FileMode.Create, FileAccess.Write)) { using (var writer = new BinaryWriterWith7BitEncodedInts(fs)) { lock (sharedSerializer) { sharedSerializer.Serialize(writer, obj); } } } } catch (Exception e) { Console.WriteLine("-----------------Serialize stack trace:"); Console.WriteLine(Environment.StackTrace); LoggingService.LogError("Error while writing type system cache. (object:" + obj.GetType() + ")", e); } finally { t.Dispose(); } }
void SaveToCache(string cacheFileName, DateTime lastWriteTime, LoadedAssembly asm) { if (cacheFileName == null) { return; } LoggingService.Debug("Serializing to " + cacheFileName); try { Directory.CreateDirectory(DomPersistencePath); using (FileStream fs = new FileStream(cacheFileName, FileMode.Create, FileAccess.Write)) { using (BinaryWriter writer = new BinaryWriterWith7BitEncodedInts(fs)) { writer.Write(lastWriteTime.Ticks); FastSerializer s = new FastSerializer(); s.Serialize(writer, asm); } } } catch (IOException ex) { LoggingService.Warn(ex); // Can happen if two SD instances are trying to access the file at the same time. // We'll just let one of them win, and instance that got the exception won't write to the cache at all. // Similarly, we also ignore the other kinds of IO exceptions. } catch (UnauthorizedAccessException ex) { LoggingService.Warn(ex); } }
public override string Serialize(Type type, object graph, object context) { using (var stream = new MemoryStream()) { Serializer.Serialize(stream, graph, context); return(stream.ToArray().GetString()); } }
public void Serialize(string xmlFilePath, TestRecord testRecord) { // no need for xml serialization because next time it will be // deserialized from the binary format string binaryFilePath = GetBinaryFilePath(xmlFilePath); using (var stream = File.OpenWrite(binaryFilePath)) { fastSerializer.Serialize(stream, testRecord); } }
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); } }
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); } }
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 void SaveToCache(string cacheFileName, IProjectContent pc) { try { Directory.CreateDirectory(Path.GetDirectoryName(cacheFileName)); using (FileStream fs = new FileStream(cacheFileName, FileMode.Create, FileAccess.Write)) { using (BinaryWriter writer = new BinaryWriterWith7BitEncodedInts(fs)) { writer.Write(cacheMagicNumber); FastSerializer s = new FastSerializer(); s.Serialize(writer, pc); } } } catch (IOException ex) { LoggingService.Warn(ex); // Can happen if two SD instances are trying to access the file at the same time. // We'll just let one of them win, and the instance that got the exception won't write to the cache at all. // Similarly, we also ignore the other kinds of IO exceptions. } catch (UnauthorizedAccessException ex) { LoggingService.Warn(ex); } }