private void Init(Type t)
        {
            UnderlyingType = t;

            TypeModule = new ModuleDescriptor(t.Module);
            if (UnderlyingType.IsGenericType)
            {
                GenericFullName = UnderlyingType.GetGenericTypeDefinition().FullName;
                Name            = UnderlyingType.GetGenericTypeDefinition().AssemblyQualifiedName;
            }
            else
            {
                Name            = UnderlyingType.AssemblyQualifiedName;
                GenericFullName = UnderlyingType.FullName;
            }

            if (t.BaseType != null)
            {
                baseType = t.BaseType;
            }

            var fieldsToDeserialize = new List <FieldInfoOrEntryToOmit>();

            foreach (var field in StampHelpers.GetFieldsInSerializationOrder(UnderlyingType, true))
            {
                fieldsToDeserialize.Add(new FieldInfoOrEntryToOmit(field));
                if (!field.IsTransient())
                {
                    fields.Add(new FieldDescriptor(field));
                }
            }
            FieldsToDeserialize = fieldsToDeserialize;
        }
        public void WriteStructureStampIfNeeded(ObjectWriter writer)
        {
            if (writer.recipes.ContainsKey(UnderlyingType))
            {
                // We do not need full stamp for recipe types.
                return;
            }

            if (StampHelpers.IsStampNeeded(this, writer.TreatCollectionAsUserObject))
            {
                WriteStructureStamp(writer);
            }
        }
        public void ReadStructureStampIfNeeded(ObjectReader reader, VersionToleranceLevel versionToleranceLevel, bool forceStampVerification = false)
        {
            if (reader.recipes.ContainsKey(UnderlyingType))
            {
                // We do not need full stamp for recipe types.
                return;
            }

            if (StampHelpers.IsStampNeeded(this, reader.TreatCollectionAsUserObject))
            {
                ReadStructureStamp(reader, versionToleranceLevel, forceStampVerification);
            }
        }
Пример #4
0
        public TypeSimpleDescriptor(Type t)
        {
            UnderlyingType  = t;
            Name            = t.AssemblyQualifiedName;
            nameAsByteArray = Encoding.UTF8.GetBytes(Name);

            var fieldsToDeserialize = new List <FieldInfoOrEntryToOmit>();

            foreach (var field in StampHelpers.GetFieldsInSerializationOrder(UnderlyingType, true))
            {
                fieldsToDeserialize.Add(new FieldInfoOrEntryToOmit(field));
            }
            FieldsToDeserialize = fieldsToDeserialize;
        }
        private List <FieldInfoOrEntryToOmit> VerifyStructure(VersionToleranceLevel versionToleranceLevel, bool forceStampVerification)
        {
            if (TypeModule.GUID != UnderlyingType.Module.ModuleVersionId)
            {
                if (!versionToleranceLevel.HasFlag(VersionToleranceLevel.AllowGuidChange))
                {
                    throw new VersionToleranceException(string.Format("The class {2} was serialized with different module version id {0}, current one is {1}.",
                                                                      TypeModule.GUID, UnderlyingType.Module.ModuleVersionId, UnderlyingType.FullName));
                }
            }
            else if (!forceStampVerification)
            {
                return(StampHelpers.GetFieldsInSerializationOrder(UnderlyingType, true).Select(x => new FieldInfoOrEntryToOmit(x)).ToList());
            }

            var result = new List <FieldInfoOrEntryToOmit>();

            var assemblyTypeDescriptor = ((TypeFullDescriptor)UnderlyingType);

            if (!(assemblyTypeDescriptor.baseType == null && baseType == null) &&
                ((assemblyTypeDescriptor.baseType == null && baseType != null) || !assemblyTypeDescriptor.baseType.Equals(baseType)) &&
                !versionToleranceLevel.HasFlag(VersionToleranceLevel.AllowInheritanceChainChange))
            {
                throw new VersionToleranceException(string.Format("Class hierarchy for {2} changed. Expected '{1}' as base class, but found '{0}'.",
                                                                  baseType != null ? baseType.UnderlyingType.FullName : "null",
                                                                  assemblyTypeDescriptor.baseType != null ? assemblyTypeDescriptor.baseType.UnderlyingType.FullName : "null",
                                                                  UnderlyingType.FullName));
            }

            if (assemblyTypeDescriptor.TypeModule.ModuleAssembly.Version != TypeModule.ModuleAssembly.Version && !versionToleranceLevel.HasFlag(VersionToleranceLevel.AllowAssemblyVersionChange))
            {
                throw new VersionToleranceException(string.Format("Assembly version changed from {0} to {1} for class {2}",
                                                                  TypeModule.ModuleAssembly.Version, assemblyTypeDescriptor.TypeModule.ModuleAssembly.Version, UnderlyingType.FullName));
            }

            var cmpResult = assemblyTypeDescriptor.CompareWith(this, versionToleranceLevel);

            if (cmpResult.FieldsChanged.Any())
            {
                throw new VersionToleranceException(string.Format("Field {0} type changed in class {1}.", cmpResult.FieldsChanged[0].Name, UnderlyingType.FullName));
            }

            if (cmpResult.FieldsAdded.Any() && !versionToleranceLevel.HasFlag(VersionToleranceLevel.AllowFieldAddition))
            {
                throw new VersionToleranceException(string.Format("Field {0} added to class {1}.", cmpResult.FieldsAdded[0].Name, UnderlyingType.FullName));
            }
            if (cmpResult.FieldsRemoved.Any() && !versionToleranceLevel.HasFlag(VersionToleranceLevel.AllowFieldRemoval))
            {
                throw new VersionToleranceException(string.Format("Field {0} removed from class {1}.", cmpResult.FieldsRemoved[0].Name, UnderlyingType.FullName));
            }

            foreach (var field in fields)
            {
                if (cmpResult.FieldsRemoved.Contains(field))
                {
                    result.Add(new FieldInfoOrEntryToOmit(field.FieldType.UnderlyingType));
                }
                else
                {
                    result.Add(new FieldInfoOrEntryToOmit(field.UnderlyingFieldInfo));
                }
            }

            foreach (var field in assemblyTypeDescriptor.GetConstructorRecreatedFields().Select(x => x.Field))
            {
                result.Add(new FieldInfoOrEntryToOmit(field));
            }

            return(result);
        }