Пример #1
0
        public override void Initialize()
        {
            var structAttribute = this.ClrType.GetCustomAttribute <FlatBufferStructAttribute>();

            if (structAttribute == null)
            {
                throw new InvalidFlatBufferDefinitionException($"Can't create struct type model from type {this.ClrType.Name} because it does not have a [FlatBufferStruct] attribute.");
            }

            TableTypeModel.EnsureClassCanBeInheritedByOutsideAssembly(this.ClrType, out this.preferredConstructor);

            var properties = this.ClrType
                             .GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
                             .Select(x => new
            {
                Property  = x,
                Attribute = x.GetCustomAttribute <FlatBufferItemAttribute>() !   // suppress check here; we filter on the next line.
            })
Пример #2
0
        public override void Initialize()
        {
            var structAttribute = this.ClrType.GetCustomAttribute <FlatBufferStructAttribute>();

            if (structAttribute == null)
            {
                throw new InvalidFlatBufferDefinitionException($"Can't create struct type model from type {this.ClrType.Name} because it does not have a [FlatBufferStruct] attribute.");
            }

            TableTypeModel.EnsureClassCanBeInheritedByOutsideAssembly(this.ClrType, out var ctor);

            var properties = this.ClrType
                             .GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
                             .Select(x => new
            {
                Property  = x,
                Attribute = x.GetCustomAttribute <FlatBufferItemAttribute>()
            })
                             .Where(x => x.Attribute != null)
                             .OrderBy(x => x.Attribute.Index);

            ushort expectedIndex = 0;

            this.inlineSize = 0;

            foreach (var item in properties)
            {
                var propertyAttribute = item.Attribute;
                var property          = item.Property;

                if (propertyAttribute.Deprecated)
                {
                    throw new InvalidFlatBufferDefinitionException($"FlatBuffer struct {this.ClrType.Name} may not have deprecated properties");
                }

                ushort index = propertyAttribute.Index;
                if (index != expectedIndex)
                {
                    throw new InvalidFlatBufferDefinitionException($"FlatBuffer struct {this.ClrType.Name} does not declare an item with index {expectedIndex}. Structs must have sequenential indexes starting at 0.");
                }

                if (!object.ReferenceEquals(propertyAttribute.DefaultValue, null))
                {
                    throw new InvalidFlatBufferDefinitionException($"FlatBuffer struct {this.ClrType.Name} declares default value on index {expectedIndex}. Structs may not have default values.");
                }

                expectedIndex++;
                ITypeModel propertyModel = this.typeModelContainer.CreateTypeModel(property.PropertyType);

                if (!propertyModel.IsValidStructMember || propertyModel.PhysicalLayout.Length > 1)
                {
                    throw new InvalidFlatBufferDefinitionException($"Struct property {property.Name} with type {property.PropertyType.Name} cannot be part of a flatbuffer struct.");
                }

                int propertySize      = propertyModel.PhysicalLayout[0].InlineSize;
                int propertyAlignment = propertyModel.PhysicalLayout[0].Alignment;
                this.maxAlignment = Math.Max(propertyAlignment, this.maxAlignment);

                // Pad for alignment.
                this.inlineSize += SerializationHelpers.GetAlignmentError(this.inlineSize, propertyAlignment);

                StructMemberModel model = new StructMemberModel(
                    propertyModel,
                    property,
                    index,
                    this.inlineSize);

                this.memberTypes.Add(model);
                this.inlineSize += propertyModel.PhysicalLayout[0].InlineSize;
            }
        }