System.IndexOutOfRangeException 从字符串转换时

System.IndexOutOfRangeException when converting from string

我有以下命名空间方法:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;

namespace ServiceLayer.Web.Core.Utilities
{
    private T GetInternal<T>(string configName)
    {
        var value = ((string) GetConfigSetting(configName));
        var conv = TypeDescriptor.GetConverter(typeof(T));
        return (T) conv.ConvertFromString(string.IsNullOrWhiteSpace(value) ? "" : value);
    }
    public string GetConfigSetting(string configName)
    {
        return ConfigurationManager.AppSettings[configName];
    }
}

旨在根据其 configName 从配置中读取一个值,例如

foo_value = applicationBase.GetConfigSetting("Foo", false);

但是 GetInternal() 中的代码在 System.IndexOutOfRangeException 中失败,当 valuenull

{System.IndexOutOfRangeException: Index was outside the bounds of the array. at System.ComponentModel.BaseNumberConverter.ConvertFrom (System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, System.Object value) [0x0001…}

at System.ComponentModel.BaseNumberConverter.ConvertFrom (System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, System.Object value) [0x00017] in /private/tmp/source-mono-d15-3/bockbuild-d15-3/profiles/mono-mac-xamarin/build-root/mono-x64/mcs/class/referencesource/System/compmod/system/componentmodel/basenumberconverter.cs:89


之前我更改了这行代码:

return (T) conv.ConvertFromString(value);

进入:

return (T) conv.ConvertFromString(string.IsNullOrWhiteSpace(value) ? "" : value);

修复之前 valuenull 时的异常:

System.NotSupportedException: Int32Converter cannot convert from (null).

at System.ComponentModel.TypeConverter.GetConvertFromException (System.Object value) [0x0001c] in /private/tmp/source-mono-d15-3/bockbuild-d15-3/profiles/mono-mac-xamarin/build-root/mono-x64/mcs/class/referencesource/System/compmod/system/componentmodel/TypeConverter.cs:260 at System.ComponentModel.TypeConverter.ConvertFrom (System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, System.Object value) [0x00011] in /private/tmp/source-mono-d15-3/bockbuild-d15-3/profiles/mono-mac-xamarin/build-root/mono-x64/mcs/class/referencesource/System/compmod/system/componentmodel/TypeConverter.cs:115 at System.ComponentModel.BaseNumberConverter.ConvertFrom (System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, System.Object value) [0x000c2] in /private/tmp/source-mono-d15-3/bockbuild-d15-3/profiles/mono-mac-xamarin/build-root/mono-x64/mcs/class/referencesource/System/compmod/system/componentmodel/basenumberconverter.cs:110 at System.ComponentModel.TypeConverter.ConvertFromString (System.String text) [0x00000] in /private/tmp/source-mono-d15-3/bockbuild-d15-3/profiles/mono-mac-xamarin/build-root/mono-x64/mcs/class/referencesource/System/compmod/system/componentmodel/TypeConverter.cs:137

但我得到了另一个。


我做错了什么以及如何解决?特别是当 valuenull?

时如何处理字符串的转换

试试这个:

return (T) conv.ConvertFromString(string.IsNullOrWhiteSpace(value) ? "0" : value);

该代码的作用是从配置文件中找到一些值,即字符串,然后从该字符串创建一些对象并将其转换为通用类型 T 并 returning 它.

如果你想要 return "number" 当它的 "number" 类型(int、short、long 等),或者 null 当它是字符串:

private T GetInternal<T>(string configName)
{ 
    string value = GetConfigSetting(configName);

    if (string.IsNullOrWhiteSpace(value))
            return default(T);

    TypeConverter conv = TypeDescriptor.GetConverter(typeof(T));
    return (T)conv.ConvertFromString(value);
}

此外,如果类型在这一点上已知,为什么要使用 var,将 GetConfigSetting() 方法转换为 string 是完全没有必要的(或者可能是 itsef 方法),因为它已经returning string。您可以只使用:

. . .
string value = ConfigurationManager.AppSettings[configName];
. . .