Beispiel #1
0
        private NamedEntityAttributes GetNamedEntityAttributesFrom(Type type)
        {
            AvroContractResolver  resolver = this.settings.Resolver;
            TypeSerializationInfo typeInfo = resolver.ResolveType(type);
            var name    = new SchemaName(typeInfo.Name, typeInfo.Namespace);
            var aliases = typeInfo
                          .Aliases
                          .Select(alias => string.IsNullOrEmpty(name.Namespace) || alias.Contains(".") ? alias : name.Namespace + "." + alias)
                          .ToList();

            return(new NamedEntityAttributes(name, aliases, typeInfo.Doc));
        }
        /// <summary>
        /// Gets the serialization information about the type.
        /// This information is used for creation of the corresponding schema node.
        /// </summary>
        /// <param name="type">The type to resolve.</param>
        /// <param name="member">The member type containing the type, if specified</param>
        /// <returns>
        /// Serialization information about the type.
        /// </returns>
        /// <exception cref="System.ArgumentNullException">The type argument is null.</exception>
        internal TypeSerializationInfo ResolveType(Type type, MemberInfo member = null)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }

            if (type.IsUnsupported())
            {
                throw new NotSupportedException(
                          string.Format(CultureInfo.InvariantCulture, "Type [{0}] is not supported by the AvroConvert.", type));
            }

            bool isNullable = this._allowNullable || type.CanContainNull() || (!type.IsValueType && member?.IsNullableReferenceType() == true);

            if (type.IsInterface() ||
                type.IsNativelySupported() ||
                (type.IsEnum() && !type.GetTypeInfo().GetCustomAttributes(false).OfType <DataContractAttribute>().Any()))
            {
                return(new TypeSerializationInfo
                {
                    Name = TypeExtensions.StripAvroNonCompatibleCharacters(type.Name),
                    Namespace = TypeExtensions.StripAvroNonCompatibleCharacters(type.Namespace),
                    Nullable = isNullable
                });
            }

            type = Nullable.GetUnderlyingType(type) ?? type;

            var attributes   = type.GetTypeInfo().GetCustomAttributes(false);
            var dataContract = attributes.OfType <DataContractAttribute>().SingleOrDefault();

            if (dataContract == null && _includeOnlyDataContractMembers)
            {
                throw new SerializationException(
                          string.Format(CultureInfo.InvariantCulture, "Type '{0}' is not supported by the resolver.", type));
            }

            var result = new TypeSerializationInfo
            {
                Name      = TypeExtensions.StripAvroNonCompatibleCharacters(dataContract?.Name ?? type.Name),
                Namespace = TypeExtensions.StripAvroNonCompatibleCharacters(dataContract?.Namespace ?? type.Namespace),
                Nullable  = isNullable,
                Doc       = attributes.OfType <DescriptionAttribute>().SingleOrDefault()?.Description
            };

            return(result);
        }