【问题标题】:System.IndexOutOfRangeException when converting from string从字符串转换时出现 System.IndexOutOfRangeException
【发布时间】:2018-03-15 18:36:27
【问题描述】:

我有以下命名空间方法:

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);

但是,当 valuenull 时,代码在 GetInternal() 中失败并出现 System.IndexOutOfRangeException。 p>

{System.IndexOutOfRangeException:索引超出了数组的范围。在 System.ComponentModel.BaseNumberConverter.ConvertFrom(System.ComponentModel.ITypeDescriptorContext 上下文,System.Globalization.CultureInfo 文化,System.Object 值)[0x0001…}

在 System.ComponentModel.BaseNumberConverter.ConvertFrom(System.ComponentModel.ITypeDescriptorContext 上下文,System.Globalization.CultureInfo 文化,System.Object 值)[0x00017] 在 /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 无法从 (null) 转换。

在 System.ComponentModel.TypeConverter.GetConvertFromException(System.Object 值)[0x0001c] 在 /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 在 System.ComponentModel.TypeConverter.ConvertFrom(System.ComponentModel.ITypeDescriptorContext 上下文,System.Globalization.CultureInfo 文化,System.Object 值)[0x00011] 在 /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 在 System.ComponentModel.BaseNumberConverter.ConvertFrom(System.ComponentModel.ITypeDescriptorContext 上下文,System.Globalization.CultureInfo 文化,System.Object 值)[0x000c2] 在 /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 在 System.ComponentModel.TypeConverter.ConvertFromString(System.String 文本)[0x00000] 在 /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时如何处理从字符串的转换?


【问题讨论】:

  • 为什么我们不简单地检查 valuenull 之前将其转换为 T 并返回 default(T) 的硬编码结果?它应该是 null 吗?
  • 你能告诉我们调用部分吗?
  • 你可以在转换价值之前尝试这样的事情:if(string.IsNullOrWhiteSpace(value)) { return default(T); }
  • 代码没有显示Stacktrace,只是[External Code],所以无法查看调用部分。我已附上图片。
  • 如果ConvertFromString() 只抛出NotSupportedExceptions,它如何抛出IndexOutOfRangeExceptionMSDN Link

标签: c# .net exception mono indexoutofboundsexception


【解决方案1】:

试试这个:

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

【讨论】:

  • 这可能修复了异常,但如果您将string 作为T 传递,您将得到0 作为结果(不是“”或null)。
  • 对三元语句稍作改动就会得到预期的结果。 return (T) conv.ConvertFromString(string.IsNullOrWhiteSpace(value) ? string.Empty : value);return (T) conv.ConvertFromString(string.IsNullOrWhiteSpace(value) ? default(string) : value);
【解决方案2】:

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

如果你想在“数字”类型(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 是完全没有必要的(或者可能是它的方法),因为它已经返回string。您可以只使用:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-05
    • 2021-02-24
    • 1970-01-01
    • 2017-12-04
    • 1970-01-01
    • 2020-03-17
    相关资源
    最近更新 更多