Example #1
0
        static Type ResolveTypeArgumentTagsWithCustomRegistry(Type memberType, Type schemaType)
        {
            schemaType = CustomTypeRegistry.GetTypeMapping(schemaType) ?? schemaType;

            if (schemaType.IsGenericType())
            {
                Type[] memberTypeArguments;

                var schemaGenericType = schemaType.GetGenericTypeDefinition();
                var memberGenericType = memberType.IsGenericType() ? memberType.GetGenericTypeDefinition() : null;

                if ((schemaGenericType == typeof(Tag.nullable <>) && memberGenericType != typeof(Nullable <>)) ||
                    (schemaGenericType == typeof(Tag.bonded <>) && memberGenericType != typeof(IBonded <>)))
                {
                    memberTypeArguments = new[] { memberType };
                }
                else
                {
                    memberTypeArguments = memberType.GetTypeInfo().GenericTypeArguments;
                }

                return(schemaGenericType.MakeGenericType(Enumerable.Zip(
                                                             memberTypeArguments,
                                                             schemaType.GetTypeInfo().GenericTypeArguments,
                                                             ResolveTypeArgumentTagsWithCustomRegistry).ToArray()));
            }

            return((schemaType == typeof(Tag.structT) || schemaType == typeof(Tag.classT)) ?
                   memberType :
                   schemaType);
        }
Example #2
0
        /// <summary>
        /// Get the Type of the schema field, including any type annotations from TypeAttribute
        /// </summary>
        /// <remarks>
        /// In some cases this may not be the actual type of the property or field.
        /// If the property or field has a TypeAttribute, this will be the attribute's value
        /// and can provide schema information that is not available on the actual
        /// property/field type.
        /// </remarks>
        public static Type GetSchemaType(this ISchemaField schemaField)
        {
            var type = schemaField.MemberType;

            // If a custom type mapping was specified via TypeAttribute, we respect it completely.
            // Otherwise, we check the type against the custom registry and try to replace all
            // instances of mapped types to their mappings.
            var typeAttr = schemaField.GetAttribute <TypeAttribute>();

            if (typeAttr != null)
            {
                type = ResolveTypeArgumentTags(type, typeAttr.Value);
            }
            else
            {
                var t = CustomTypeRegistry.ResolveFieldType(schemaField);
                if (t != null)
                {
                    type = t;
                }
                else
                {
                    type = ResolveTypeArgumentTagsWithCustomRegistry(type, type);
                }
            }

            return(type);
        }
Example #3
0
        internal static string GetSchemaName(this Type type)
        {
            Type Alias;

            if ((Alias = CustomTypeRegistry.GetTypeMapping(type)) != null)
            {
                return(GetSchemaName(Alias));
            }

            string name;

            if (type.IsBondStruct() || type.IsEnum())
            {
                name = type.Name;
                var n = name.IndexOf('`');
                if (n >= 0)
                {
                    name = name.Remove(n);
                }
            }
            else if (type.IsBondBlob())
            {
                return("blob");
            }
            else if (type.IsBonded())
            {
                name = "bonded";
            }
            else if (type.IsBondNullable())
            {
                name = "nullable";
            }
            else
            {
                name = bondTypeName[type.GetBondDataType()];
            }

            if (!type.IsGenericType())
            {
                return(name);
            }

            var args    = type.GetTypeInfo().GenericTypeArguments;
            var builder = new StringBuilder(name, args.Length * 64);

            builder.Append("<");
            for (var i = 0; i < args.Length; ++i)
            {
                if (i != 0)
                {
                    builder.Append(", ");
                }

                builder.Append(args[i].GetSchemaFullName());
            }
            builder.Append(">");
            return(builder.ToString());
        }