Пример #1
0
        public TypeDetailsInformation GetTypeInformation(Type type, object value, IMemberInfoWrapper memberInfoWrapper)
        {
            var typeInformation = GetBaseTypeInformation(type, memberInfoWrapper);

            if (value != null)
            {
                return(typeInformation);
            }

            if (typeInformation.TypeCode == TypeCode.ComplexObject)
            {
                return(new TypeDetailsInformation(TypeCode.NullValue,
                                                  new UnderlyingTypeInformation(typeInformation.TypeCode,
                                                                                typeInformation.UnderlyingTypeInformation.IsEnum,
                                                                                typeInformation.UnderlyingTypeInformation.Type),
                                                  null,
                                                  memberInfoWrapper.GetMemberType()));
            }

            return(new TypeDetailsInformation(TypeCode.NullValue, null, null, memberInfoWrapper.GetMemberType()));
        }
 public Type GetMemberType()
 {
     return(_infoWrapper.GetMemberType());
 }
Пример #3
0
        public object ReadValue(IMemberInfoWrapper memberInfoWrapper, object configuration, Type configurationType)
        {
            var(converter, defaultConverter, typeDetailsInformation) = _iniConverterFactory.GetConverter(memberInfoWrapper.GetMemberType(), 0, memberInfoWrapper);

            if (typeDetailsInformation.TypeCode == TypeCode.ComplexObject)
            {
                return(converter.ParseReadValue(null, typeDetailsInformation.Type, null));
            }

            var iniValue = new IniValue()
            {
                Section = _iniValueManager.GetSection(configurationType, memberInfoWrapper),
                Key     = _iniValueManager.GetKey(memberInfoWrapper)
            };

            try
            {
                var iniContext = new IniContext(memberInfoWrapper, typeDetailsInformation, iniValue, _iniParser, defaultConverter);

                var readValue = _iniParser.Read(iniValue.Section, iniValue.Key);

                return(converter.ParseReadValue(readValue, typeDetailsInformation.Type, iniContext));
            }
            catch (FormatException)
            {
                throw new IniWrongFormatException($"Wrong format in {iniValue} expected type: {memberInfoWrapper.GetMemberType()}");
            }
        }
Пример #4
0
        private TypeDetailsInformation GetBaseTypeInformation(Type type, IMemberInfoWrapper memberInfoWrapper)
        {
            if (!IsNullableType(type) && TypeCodeMap.TryGetValue(type, out var typeCode))
            {
                return(new TypeDetailsInformation(typeCode, new UnderlyingTypeInformation(typeCode, false, type), null, memberInfoWrapper.GetMemberType()));
            }

            if (IsDictionaryType(type))
            {
                var underlyingGenericTypeKey   = type.GenericTypeArguments[0];
                var underlyingGenericTypeValue = type.GenericTypeArguments[1];

                var genericKeyTypeCode   = GetBaseTypeInformation(underlyingGenericTypeKey, memberInfoWrapper);
                var genericValueTypeCode = GetBaseTypeInformation(underlyingGenericTypeValue, memberInfoWrapper);

                var underlyingKeyTypeInformation = new UnderlyingTypeInformation(genericKeyTypeCode.TypeCode, genericKeyTypeCode.UnderlyingTypeInformation.IsEnum, genericKeyTypeCode.UnderlyingTypeInformation.Type);
                var underlyingTypeInformation    = new UnderlyingTypeInformation(genericValueTypeCode.TypeCode, genericValueTypeCode.UnderlyingTypeInformation.IsEnum, genericValueTypeCode.UnderlyingTypeInformation.Type);

                return(new TypeDetailsInformation(TypeCode.Dictionary, underlyingTypeInformation, underlyingKeyTypeInformation, memberInfoWrapper.GetMemberType()));
            }

            if (typeof(IEnumerable).IsAssignableFrom(type))
            {
                var underlyingGenericType = type.GenericTypeArguments[0];
                var genericTypeCode       = GetBaseTypeInformation(underlyingGenericType, memberInfoWrapper);

                return(new TypeDetailsInformation(TypeCode.Enumerable,
                                                  new UnderlyingTypeInformation(
                                                      genericTypeCode.TypeCode,
                                                      genericTypeCode.UnderlyingTypeInformation.IsEnum,
                                                      genericTypeCode.UnderlyingTypeInformation.Type),
                                                  null,
                                                  memberInfoWrapper.GetMemberType()));
            }

            if (type.IsEnum)
            {
                var underlyingType         = Enum.GetUnderlyingType(type);
                var typeDetailsInformation = GetBaseTypeInformation(underlyingType, memberInfoWrapper);

                return(new TypeDetailsInformation(typeDetailsInformation.TypeCode,
                                                  new UnderlyingTypeInformation(TypeCode.Empty,
                                                                                true,
                                                                                type),
                                                  null,
                                                  memberInfoWrapper.GetMemberType()));
            }

            var nullable = Nullable.GetUnderlyingType(type);

            if (nullable == null)
            {
                return(new TypeDetailsInformation(TypeCode.ComplexObject,
                                                  new UnderlyingTypeInformation(TypeCode.Empty,
                                                                                false,
                                                                                type),
                                                  null,
                                                  memberInfoWrapper.GetMemberType()));
            }

            if (nullable.IsEnum)
            {
                var nullableUnderlyingType = Enum.GetUnderlyingType(nullable);
                var underlyingType         = GetBaseTypeInformation(nullableUnderlyingType, memberInfoWrapper);

                return(new TypeDetailsInformation(underlyingType.TypeCode,
                                                  new UnderlyingTypeInformation(TypeCode.Empty,
                                                                                true,
                                                                                nullableUnderlyingType),
                                                  null,
                                                  nullable));
            }

            return(new TypeDetailsInformation(TypeCode.Nullable,
                                              new UnderlyingTypeInformation(TypeCode.Empty,
                                                                            false,
                                                                            nullable),
                                              null,
                                              nullable));
        }
Пример #5
0
        public void SaveValue(IMemberInfoWrapper memberInfoWrapper, object configuration)
        {
            var value = memberInfoWrapper.GetValue(configuration);

            var defaultIniValue = new IniValue()
            {
                Section = _iniValueManager.GetSection(configuration.GetType(), memberInfoWrapper),
                Key     = _iniValueManager.GetKey(memberInfoWrapper),
            };

            var(converter, defaultConverter, typeDetailsInformation) = _iniConverterFactory.GetConverter(memberInfoWrapper.GetMemberType(), value, memberInfoWrapper);

            var iniContext = new IniContext(memberInfoWrapper, typeDetailsInformation, defaultIniValue, _iniParser, defaultConverter);

            var valueToSave = converter.FormatToWrite(value, iniContext);

            if (valueToSave?.Value == null)
            {
                return;
            }

            _iniParser.Write(valueToSave.Section, valueToSave.Key, valueToSave.Value);
        }