public override CodeGeneratedMethod CreateParseMethodBody(ParserCodeGenContext context) { var(classDef, className) = FlatBufferVectorHelpers.CreateFlatBufferVectorOfUnionSubclass( this.ItemTypeModel, context); string createFlatBufferVector = $@"new {className}<{context.InputBufferTypeName}>( {context.InputBufferVariableName}, {context.OffsetVariableName}.offset0 + {context.InputBufferVariableName}.{nameof(InputBufferExtensions.ReadUOffset)}({context.OffsetVariableName}.offset0), {context.OffsetVariableName}.offset1 + {context.InputBufferVariableName}.{nameof(InputBufferExtensions.ReadUOffset)}({context.OffsetVariableName}.offset1), {context.TableFieldContextVariableName})"; return(new CodeGeneratedMethod(ListVectorTypeModel.CreateParseBody( this.ItemTypeModel, createFlatBufferVector, context)) { ClassDefinition = classDef }); }
/// <summary> /// Tries to create a type model based on the given type. /// </summary> public bool TryCreateTypeModel(TypeModelContainer container, Type type, [NotNullWhen(true)] out ITypeModel?typeModel) { if (type == typeof(string)) { typeModel = new StringTypeModel(container); return(true); } if (type.IsArray) { if (typeof(IFlatBufferUnion).IsAssignableFrom(type.GetElementType())) { typeModel = new ArrayVectorOfUnionTypeModel(type, container); } else { typeModel = new ArrayVectorTypeModel(type, container); } return(true); } if (type.IsGenericType) { var genericDef = type.GetGenericTypeDefinition(); if (genericDef == typeof(IList <>) || genericDef == typeof(IReadOnlyList <>)) { if (typeof(IFlatBufferUnion).IsAssignableFrom(type.GetGenericArguments()[0])) { typeModel = new ListVectorOfUnionTypeModel(type, container); } else { typeModel = new ListVectorTypeModel(type, container); } return(true); } if (genericDef == typeof(Memory <>) || genericDef == typeof(ReadOnlyMemory <>)) { typeModel = new MemoryVectorTypeModel(type, container); return(true); } if (genericDef == typeof(IIndexedVector <,>)) { typeModel = new IndexedVectorTypeModel(type, container); return(true); } } if (typeof(IFlatBufferUnion).IsAssignableFrom(type)) { typeModel = new UnionTypeModel(type, container); return(true); } if (type.IsEnum) { typeModel = new EnumTypeModel(type, container); return(true); } var underlyingType = Nullable.GetUnderlyingType(type); if (underlyingType is not null) { typeModel = new NullableTypeModel(container, type); return(true); } var tableAttribute = type.GetCustomAttribute <FlatBufferTableAttribute>(); var structAttribute = type.GetCustomAttribute <FlatBufferStructAttribute>(); if (tableAttribute is not null && structAttribute is not null) { throw new InvalidFlatBufferDefinitionException($"Type '{CSharpHelpers.GetCompilableTypeName(type)}' is declared as both [FlatBufferTable] and [FlatBufferStruct]."); }