从string转换时出现System.IndexOutOfRangeException

时间:2017-10-04 11:26:22

标签: c# .net exception mono indexoutofboundsexception

我有以下命名空间方法:

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

然而,当 null 时,代码在 GetInternal()中失败并带有 System.IndexOutOfRangeException

  

{System.IndexOutOfRangeException:索引超出了数组的范围。在System.ComponentModel.BaseNumberConverter.ConvertFrom(System.ComponentModel.ITypeDescriptorContext context,System.Globalization.CultureInfo culture,System.Object value)[0x0001 ...}

     

在/ private / tmp / source-mono-d15-3 / bockbuild中的System.ComponentModel.BaseNumberConverter.ConvertFrom(System.ComponentModel.ITypeDescriptorContext context,System.Globalization.CultureInfo culture,System.Object value)[0x00017] d15-3 /简档/单MAC-xamarin /建立根/单X64 / MCS /类/ referencesource /系统/ compmod /系统/ componentmodel / basenumberconverter.cs:89

以前我改变了这行代码:

return (T) conv.ConvertFromString(value);

成:

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

null 时修复上一个异常:

  

System.NotSupportedException:Int32Converter无法转换为(null)。

     

在/ private / tmp / source-mono-d15-3 / bockbuild-d15-3 / profiles / mono-mac-xamarin / build-中的System.ComponentModel.TypeConverter.GetConvertFromException(System.Object value)[0x0001c]根/单X64 / MCS /类/ referencesource /系统/ compmod /系统/ componentmodel / TypeConverter.cs:260     在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     在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     在/ private / tmp / source-mono-d15-3 / bockbuild-d15-3 / profiles / mono-mac-xamarin / build-root / mono中的System.ComponentModel.TypeConverter.ConvertFromString(System.String text)[0x00000] -x64 / mcs / class / referencesource / System / compmod / system / componentmodel / TypeConverter.cs:137

但我得到了另一个。

我做错了什么以及如何解决?特别是当 null 时如何处理字符串的转换?

C#: {System.IndexOutOfRangeException: Index was outside the bounds of the array

C#: {System.IndexOutOfRangeException: Index was outside the bounds of the array

2 个答案:

答案 0 :(得分:1)

试试这个:

initial

答案 1 :(得分:1)

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

你可以使用这种方法,如果你想要返回&#34;数字&#34;当它的数字&#34;类型(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将该方法强制转换),因为它&#39;已经返回string。你可以使用:

. . .
string value = ConfigurationManager.AppSettings[configName];
. . .
相关问题