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);
			}
		}
		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()
		{
			IkvmLoader loader = new IkvmLoader() { 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, IkvmLoaderTests.Mscorlib);
			}
		}
 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);
     }
 }
 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;
     }
 }
		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 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);
			}
		}
		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;
			}
		}
Example #8
0
			internal SerializationContext(FastSerializer fastSerializer, BinaryWriter writer)
			{
				this.fastSerializer = fastSerializer;
				this.writer = writer;
				instances.Add(null); // use object ID 0 for null
				objectTypes.Add(null);
			}
Example #9
0
			internal void DeserializeTypeDescriptions(FastSerializer fastSerializer)
			{
				for (int i = 0; i < this.Types.Length; i++) {
					Type type = this.Types[i];
					if (type.IsGenericTypeDefinition || type.HasElementType)
						continue;
					bool isCustomSerialization = typeof(ISerializable).IsAssignableFrom(type);
					bool typeIsSpecial = type.IsPrimitive || isCustomSerialization;
					
					byte serializedFieldCount = Reader.ReadByte();
					if (serializedFieldCount == byte.MaxValue) {
						// special type
						if (!typeIsSpecial)
							throw new SerializationException("Type " + type + " was serialized as special type, but isn't special now.");
					} else {
						if (typeIsSpecial)
							throw new SerializationException("Type " + type.FullName + " wasn't serialized as special type, but is special now.");
						
						var availableFields = GetSerializableFields(this.Types[i]);
						if (availableFields.Count != serializedFieldCount)
							throw new SerializationException("Number of fields on " + type.FullName + " has changed.");
						for (int j = 0; j < serializedFieldCount; j++) {
							int fieldTypeID = ReadTypeID();
							
							string fieldName = Reader.ReadString();
							FieldInfo fieldInfo = availableFields[j];
							if (fieldInfo.Name != fieldName)
								throw new SerializationException("Field mismatch on type " + type.FullName);
							if (fieldInfo.FieldType != this.Types[fieldTypeID])
								throw new SerializationException(type.FullName + "." + fieldName + " was serialized as " + this.Types[fieldTypeID] + ", but now is " + fieldInfo.FieldType);
						}
					}
				}
			}