Пример #1
0
        public override string ToString()
        {
            if (!HasValue)
            {
                return(string.Empty);
            }

            if (Value is string sValue)
            {
                return(sValue);
            }

            if (TypeUtils.IsPrimitive(Value))
            {
                return(StringConvertor.ToString(Value, _config.Culture));
            }

            var convertor = _config.ObjectConvertor;

            if (convertor == null)
            {
                throw new SettingException($"Type {Value.GetType().FullName} is not primitive therefore {nameof(ISettings.Config.ObjectConvertor)}.{nameof(ISettingsConfig.ObjectConvertor)} must be specified.");
            }

            return(convertor.ToString(Value, _config.Culture));
        }
Пример #2
0
        public T ConvertValue <T>()
        {
            if (Value is T tValue)
            {
                return(tValue);
            }

            if (Value is string sValue && TypeUtils.IsPrimitive(typeof(T)))
            {
                return(StringConvertor.FromString <T>(sValue, _config.Culture));
            }

            var convertor = _config.ObjectConvertor;

            if (convertor == null)
            {
                throw new SettingException($"Type {typeof(T)} is not primitive therefore {nameof(ISettings.Config.ObjectConvertor)}.{nameof(ISettingsConfig.ObjectConvertor)} must be specified.");
            }

            return(convertor.From <T>(Value, _config.Culture));
        }