/// <summary>
        /// Tries to create a type model based on the given type.
        /// </summary>
        public bool TryCreateTypeModel(TypeModelContainer container, Type type, out ITypeModel typeModel)
        {
            if (type == typeof(string))
            {
                typeModel = new StringTypeModel();
                return(true);
            }

            if (type == typeof(SharedString))
            {
                typeModel = new SharedStringTypeModel();
                return(true);
            }

            if (type.IsArray)
            {
                typeModel = new ArrayVectorTypeModel(type, container);
                return(true);
            }

            if (type.IsGenericType)
            {
                var genericDef = type.GetGenericTypeDefinition();
                if (genericDef == typeof(IList <>) || genericDef == typeof(IReadOnlyList <>))
                {
                    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(IUnion).IsAssignableFrom(type))
            {
                typeModel = new UnionTypeModel(type, container);
                return(true);
            }

            if (type.IsEnum)
            {
                typeModel = new EnumTypeModel(type, container);
                return(true);
            }

            if (Nullable.GetUnderlyingType(type) != null)
            {
                var underlyingType = Nullable.GetUnderlyingType(type);
                if (underlyingType.IsEnum)
                {
                    typeModel = new NullableEnumTypeModel(type, container);
                    return(true);
                }
            }

            var tableAttribute = type.GetCustomAttribute <FlatBufferTableAttribute>();

            if (tableAttribute != null)
            {
                typeModel = new TableTypeModel(type, container);
                return(true);
            }

            var structAttribute = type.GetCustomAttribute <FlatBufferStructAttribute>();

            if (structAttribute != null)
            {
                typeModel = new StructTypeModel(type, container);
                return(true);
            }

            typeModel = null;
            return(false);
        }
示例#2
0
        /// <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 == typeof(SharedString))
            {
                typeModel = new SharedStringTypeModel(container);
                return(true);
            }

            if (type.IsArray)
            {
                typeModel = new ArrayVectorTypeModel(type, container);
                return(true);
            }

            if (type.IsGenericType)
            {
                var genericDef = type.GetGenericTypeDefinition();
                if (genericDef == typeof(IList <>) || genericDef == typeof(IReadOnlyList <>))
                {
                    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(IUnion).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].");
            }