Ejemplo n.º 1
0
        /// <summary>
        /// Initializes a new instance of the ThriftType class from the specified .NET type.
        /// </summary>
        private ThriftType(Type type)
        {
            Id       = ThriftTypeId.Empty;
            TypeInfo = type.GetTypeInfo();

            if (type == typeof(void))
            {
                return;
            }

            var underlyingNullableType = Nullable.GetUnderlyingType(type);

            if (underlyingNullableType != null)
            {
                NullableType = ThriftType.Get(underlyingNullableType);
                Id           = NullableType.Id;
                IsEnum       = NullableType.IsEnum;
                return;
            }

            if (PrimitiveIds.ContainsKey(type))
            {
                Id = PrimitiveIds[type];
                return;
            }

            if (TypeInfo.IsEnum)
            {
                if (TypeInfo.GetCustomAttribute <ThriftEnumAttribute>() == null)
                {
                    throw ThriftParsingException.EnumWithoutAttribute(TypeInfo);
                }
                if (Enum.GetUnderlyingType(type) != typeof(int))
                {
                    throw ThriftParsingException.NonInt32Enum(TypeInfo);
                }

                Id     = ThriftTypeId.Int32;
                IsEnum = true;
                return;
            }

            if (TypeInfo.IsValueType)
            {
                throw ThriftParsingException.UnknownValueType(TypeInfo);
            }

            if (TypeInfo.IsArray)
            {
                Id = ThriftTypeId.List;
                return;
            }

            var mapInterfaceAndArgs = GetInstantiableVersion(TypeInfo, typeof(IDictionary <,>), typeof(Dictionary <,>), p => ThriftParsingException.UnsupportedMap(p));

            if (mapInterfaceAndArgs != null)
            {
                Id       = ThriftTypeId.Map;
                TypeInfo = mapInterfaceAndArgs.Item1;
                _collectionGenericArgs = mapInterfaceAndArgs.Item2;
            }

            var setInterfaceAndArgs = GetInstantiableVersion(TypeInfo, typeof(ISet <>), typeof(HashSet <>), p => ThriftParsingException.UnsupportedSet(p));

            if (setInterfaceAndArgs != null)
            {
                if (mapInterfaceAndArgs != null)
                {
                    throw ThriftParsingException.CollectionWithOrthogonalInterfaces(TypeInfo);
                }

                Id       = ThriftTypeId.Set;
                TypeInfo = setInterfaceAndArgs.Item1;
                _collectionGenericArgs = setInterfaceAndArgs.Item2;
            }

            var listInterfaceAndArgs = GetInstantiableVersion(TypeInfo, typeof(IList <>), typeof(List <>), p => ThriftParsingException.UnsupportedList(p));

            if (listInterfaceAndArgs != null)
            {
                if (mapInterfaceAndArgs != null || setInterfaceAndArgs != null)
                {
                    throw ThriftParsingException.CollectionWithOrthogonalInterfaces(TypeInfo);
                }

                Id       = ThriftTypeId.List;
                TypeInfo = listInterfaceAndArgs.Item1;
                _collectionGenericArgs = listInterfaceAndArgs.Item2;
            }

            if (Id == ThriftTypeId.Empty)
            {
                Id = ThriftTypeId.Struct;
            }
        }