public void LoadTypeDescriptors(AbstractBufferedReader reader) { var typeId = reader.ReadVUInt32(); var firstTypeId = typeId; while (typeId != 0) { if (typeId < firstTypeId) { firstTypeId = typeId; } var typeCategory = (TypeCategory)reader.ReadUInt8(); ITypeDescriptor descriptor; switch (typeCategory) { case TypeCategory.BuildIn: throw new ArgumentOutOfRangeException(); case TypeCategory.Class: descriptor = new ObjectTypeDescriptor(_typeSerializers, reader, NestedDescriptorReader); break; case TypeCategory.List: descriptor = new ListTypeDescriptor(_typeSerializers, reader, NestedDescriptorReader); break; case TypeCategory.Dictionary: descriptor = new DictionaryTypeDescriptor(_typeSerializers, reader, NestedDescriptorReader); break; case TypeCategory.Enum: descriptor = new EnumTypeDescriptor(_typeSerializers, reader); break; case TypeCategory.Nullable: descriptor = new NullableTypeDescriptor(_typeSerializers, reader, NestedDescriptorReader); break; default: throw new ArgumentOutOfRangeException(); } while (typeId >= _id2DescriptorMap.Count) { _id2DescriptorMap.Add(null); } if (_id2DescriptorMap[(int)typeId] == null) { _id2DescriptorMap[(int)typeId] = new InfoForType { Id = (int)typeId, Descriptor = descriptor } } ; typeId = reader.ReadVUInt32(); } for (var i = firstTypeId; i < _id2DescriptorMap.Count; i++) { _id2DescriptorMap[(int)i].Descriptor.MapNestedTypes(d => { var placeHolderDescriptor = d as PlaceHolderDescriptor; return(placeHolderDescriptor != null ? _id2DescriptorMap[(int)placeHolderDescriptor.TypeId].Descriptor : d); }); } // This additional cycle is needed to fill names of recursive structures for (var i = firstTypeId; i < _id2DescriptorMap.Count; i++) { _id2DescriptorMap[(int)i].Descriptor.MapNestedTypes(d => d); } for (var i = firstTypeId; i < _id2DescriptorMap.Count; i++) { var infoForType = _id2DescriptorMap[(int)i]; var descriptor = _typeSerializers.MergeDescriptor(infoForType.Descriptor); infoForType.Descriptor = descriptor; _typeOrDescriptor2Info[descriptor] = infoForType; } } ITypeDescriptor NestedDescriptorReader(AbstractBufferedReader reader) { var typeId = reader.ReadVUInt32(); if (typeId < _id2DescriptorMap.Count) { var infoForType = _id2DescriptorMap[(int)typeId]; if (infoForType != null) { return(infoForType.Descriptor); } } return(new PlaceHolderDescriptor(typeId)); }
public ITypeDescriptor?Create(Type type) { if (_type2DescriptorMap.TryGetValue(type, out var result)) { return(result); } if (_temporaryMap.TryGetValue(type, out result)) { return(result); } if (!type.IsSubclassOf(typeof(Delegate))) { if (type.IsGenericType) { if (type.InheritsOrImplements(typeof(IList <>)) || type.InheritsOrImplements(typeof(ISet <>))) { result = new ListTypeDescriptor(_typeSerializers, type); } else if (type.InheritsOrImplements(typeof(IDictionary <,>))) { result = new DictionaryTypeDescriptor(_typeSerializers, type); } else if (_typeSerializers._options.IgnoreIIndirect && type.InheritsOrImplements(typeof(IIndirect <>))) { return(null); } else if (Nullable.GetUnderlyingType(type) != null) { result = new NullableTypeDescriptor(_typeSerializers, type); } else { result = new ObjectTypeDescriptor(_typeSerializers, type); } } else if (type.IsArray) { result = new ListTypeDescriptor(_typeSerializers, type); } else if (type.IsEnum) { result = new EnumTypeDescriptor(_typeSerializers, type); } else if (type.IsValueType) { throw new BTDBException($"Unsupported value type {type.Name}."); } else { result = new ObjectTypeDescriptor(_typeSerializers, type); } } _temporaryMap[type] = result; if (result != null) { if (!result.FinishBuildFromType(this)) { _temporaryMap.Remove(type); return(null); } } return(result); }
public TypeNewDescriptorGenerator(NullableTypeDescriptor nullableTypeDescriptor) { _nullableTypeDescriptor = nullableTypeDescriptor; }