private SerializableType Build(Type type) { var properties = type.GetTypeInfo().GetProperties(PropertyFlags); UInt32 nextIndex = 1; var serializableProperties = new Dictionary <string, SerializableProperty>(); foreach (var property in properties) { if (!_inspector.CanBeSerialized(type, property)) { continue; } if (serializableProperties.ContainsKey(property.Name)) { throw InvalidGraphException.DuplicateProperties(type, property); } var metadata = _inspector.AcquirePropertyMetadata(type, property, ref nextIndex); var ser = new SerializableProperty(property, metadata, Provider); serializableProperties.Add(property.Name, ser); } return(new SerializableType(type, serializableProperties)); }
public ChildTravellerInfo GetTraveller(Type type) { ChildTravellerInfo childTravellerInfo; if (!_childTravellers.TryGetValue(type, out childTravellerInfo)) { throw InvalidGraphException.ComplexTypeWithoutTravellerDefined(type); } return(childTravellerInfo); }
public CollectionMembers(ExtendedType collectionType) { ArrayContainerTypeInfo arrayTypeInfo; if (collectionType.TryGetArrayTypeInfo(out arrayTypeInfo)) { if (arrayTypeInfo.Ranks > 3) { throw new NotSupportedException("The serialization engine is limited to 3 ranks in arrays"); } if (arrayTypeInfo.Ranks == 3) { var baseType = typeof(ICollection <>); ElementType = baseType.MakeGenericType(baseType.MakeGenericType(arrayTypeInfo.ElementType)); ToArray = typeof(ArrayProvider).GetTypeInfo().GetMethod("To3DArray").MakeGenericMethod(arrayTypeInfo.ElementType); } else if (arrayTypeInfo.Ranks == 2) { ElementType = typeof(ICollection <>).MakeGenericType(arrayTypeInfo.ElementType); ToArray = typeof(ArrayProvider).GetTypeInfo().GetMethod("To2DArray").MakeGenericMethod(arrayTypeInfo.ElementType); } else { ElementType = arrayTypeInfo.ElementType; ToArray = typeof(ArrayProvider).GetTypeInfo().GetMethod("ToArray").MakeGenericMethod(arrayTypeInfo.ElementType); } } else { ElementType = collectionType.Container.AsCollection().ElementType; } ElementTypeExt = collectionType.Provider.Extend(ElementType); VariableType = typeof(ICollection <>).MakeGenericType(ElementType); Add = VariableType.GetTypeInfo().GetMethod("Add", new[] { ElementType }); var instanceType = collectionType.Info.IsInterface || collectionType.Ref.IsArray ? typeof(List <>).MakeGenericType(ElementType) : collectionType.Ref; Constructor = instanceType.GetTypeInfo().GetConstructor(Type.EmptyTypes); if (Constructor == null) { throw InvalidGraphException.NoParameterLessConstructor(collectionType.Ref); } }
private void GenerateCreateAndChildCallCode(ILLocalVariable local) { var type = local.Type; var constructor = type.GetTypeInfo().GetConstructor(Type.EmptyTypes); if (constructor == null) { throw InvalidGraphException.NoParameterLessConstructor(type); } _il.Construct(constructor); _il.Set(local); var childTravellerInfo = _context.GetTraveller(type); var field = ILPointer.Field(ILPointer.This(), childTravellerInfo.Field); _il.InvokeMethod(field, childTravellerInfo.TravelReadMethod, _visitorVariable, local); }
public DictionaryMembers(ExtendedType dictionaryType) { var container = dictionaryType.Container.AsDictionary(); KeyType = container.KeyType; ValueType = container.ValueType; ElementType = container.ElementType; VariableType = typeof(IDictionary <,>).MakeGenericType(KeyType, ValueType); Add = VariableType.GetTypeInfo().GetMethod("Add", new[] { KeyType, ValueType }); var instanceType = dictionaryType.Info.IsInterface ? typeof(Dictionary <,>).MakeGenericType(KeyType, ValueType) : dictionaryType.Ref; Constructor = instanceType.GetTypeInfo().GetConstructor(Type.EmptyTypes); if (Constructor == null) { throw InvalidGraphException.NoParameterLessConstructor(dictionaryType.Ref); } }
public void Travel(IReadVisitor visitor, DataBlock graph) { Guid?v0; if (visitor.TryVisitValue(_argsId, out v0) && v0.HasValue) { graph.Id = v0.Value; } String v1; if (visitor.TryVisitValue(_argsString, out v1)) { graph.String = v1; } Int16?v2; if (visitor.TryVisitValue(_argsInt16, out v2) && v2.HasValue) { graph.Int16 = v2.Value; } Int32?v3; if (visitor.TryVisitValue(_argsInt32, out v3) && v3.HasValue) { graph.Int32 = v3.Value; } Int64?v4; if (visitor.TryVisitValue(_argsInt64, out v4) && v4.HasValue) { graph.Int64 = v4.Value; } UInt16?v5; if (visitor.TryVisitValue(_argsUInt16, out v5) && v5.HasValue) { graph.UInt16 = v5.Value; } UInt32?v6; if (visitor.TryVisitValue(_argsUInt32, out v6) && v6.HasValue) { graph.UInt32 = v6.Value; } UInt64?v7; if (visitor.TryVisitValue(_argsUInt64, out v7) && v7.HasValue) { graph.UInt64 = v7.Value; } Single?v8; if (visitor.TryVisitValue(_argsSingle, out v8) && v8.HasValue) { graph.Single = v8.Value; } Double?v9; if (visitor.TryVisitValue(_argsDouble, out v9) && v9.HasValue) { graph.Double = v9.Value; } TimeSpan?v10; if (visitor.TryVisitValue(_argsTimeSpan, out v10) && v10.HasValue) { graph.TimeSpan = v10.Value; } Decimal?v11; if (visitor.TryVisitValue(_argsDecimal, out v11) && v11.HasValue) { graph.Decimal = v11.Value; } DateTime?v12; if (visitor.TryVisitValue(_argsDateTime, out v12) && v12.HasValue) { graph.DateTime = v12.Value; } Byte?v13; if (visitor.TryVisitValue(_argsByte, out v13) && v13.HasValue) { graph.Byte = v13.Value; } Boolean?v14; if (visitor.TryVisitValue(_argsBoolean, out v14) && v14.HasValue) { graph.Boolean = v14.Value; } Byte[] v15; if (visitor.TryVisitValue(_argsBlob, out v15)) { graph.Blob = v15; } ValueState state; state = visitor.TryVisit(_argsMessages); if (state != ValueState.NotFound) { if (state == ValueState.Found) { var c = new List <string>(); string cv; while (visitor.TryVisitValue(VisitArgs.CollectionItem, out cv) && cv != null) { c.Add(cv); } graph.Messages = c; visitor.Leave(_argsMessages); } else { graph.Messages = null; } } state = visitor.TryVisit(_argsStamps); if (state != ValueState.NotFound) { if (state == ValueState.Found) { var c = new List <DateTime>(); DateTime?cv; while (visitor.TryVisitValue(VisitArgs.CollectionItem, out cv) && cv.HasValue) { c.Add(cv.Value); } graph.Stamps = c; visitor.Leave(_argsStamps); } else { graph.Stamps = null; } } state = visitor.TryVisit(_argsRelation); if (state != ValueState.NotFound) { if (state == ValueState.Found) { var c = new Relation(); _travellerRelation0.Travel(visitor, c); graph.Relation = c; visitor.Leave(_argsRelation); } else { graph.Relation = null; } } state = visitor.TryVisit(_argsDummyRelation); if (state != ValueState.NotFound) { if (state == ValueState.Found) { var c = new Relation(); _travellerRelation0.Travel(visitor, c); graph.DummyRelation = c; visitor.Leave(_argsDummyRelation); } else { graph.DummyRelation = null; } } state = visitor.TryVisit(_argsSecondaryRelations); if (state != ValueState.NotFound) { if (state == ValueState.Found) { var c = new List <Relation>(); while (visitor.TryVisit(VisitArgs.CollectionItem) == ValueState.Found) { var cv = new Relation(); _travellerRelation0.Travel(visitor, cv); visitor.Leave(VisitArgs.CollectionItem); c.Add(cv); } graph.SecondaryRelations = c; visitor.Leave(_argsSecondaryRelations); } else { graph.SecondaryRelations = null; } } state = visitor.TryVisit(_argsIndexedValues); if (state != ValueState.NotFound) { if (state == ValueState.Found) { IDictionary <string, int> c = new Dictionary <string, int>(); string ck; while (visitor.TryVisitValue(VisitArgs.DictionaryKey, out ck) && ck != null) { int?cv; if (visitor.TryVisitValue(VisitArgs.DictionaryValue, out cv) && cv.HasValue) { c.Add(ck, cv.Value); } else { throw InvalidGraphException.NoDictionaryValue("IndexedValues"); } } graph.IndexedValues = (Dictionary <string, int>)c; visitor.Leave(_argsIndexedValues); } else { graph.IndexedValues = null; } } state = visitor.TryVisit(_argsCategories); if (state != ValueState.NotFound) { if (state == ValueState.Found) { IDictionary <Identifier, Category> c = new Dictionary <Identifier, Category>(); while (visitor.TryVisit(VisitArgs.DictionaryKey) == ValueState.Found) { var ck = new Identifier(); _travellerIdentifier1.Travel(visitor, ck); visitor.Leave(VisitArgs.DictionaryKey); if (visitor.TryVisit(VisitArgs.DictionaryValue) == ValueState.Found) { var cv = new Category(); _travellerCategory2.Travel(visitor, cv); visitor.Leave(VisitArgs.DictionaryValue); c.Add(ck, cv); } else { throw InvalidGraphException.NoDictionaryValue("Categories"); } } graph.Categories = (Dictionary <Identifier, Category>)c; visitor.Leave(_argsCategories); } else { graph.IndexedValues = null; } } }