Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从字符串转换时System.IndexOutOfRangeException_C#_.net_Exception_Mono_Indexoutofboundsexception - Fatal编程技术网

C# 从字符串转换时System.IndexOutOfRangeException

C# 从字符串转换时System.IndexOutOfRangeException,c#,.net,exception,mono,indexoutofboundsexception,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

我有以下名称空间方法:

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];
    }
}
但是,当值为null时,代码在带有System.IndexOutOfRangeException的GetInternal()中失败

{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]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);
进入:

要修复值为null时的上一个异常,请执行以下操作:

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

在/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.GetConvertFromException(System.Object值)[0x0001c] 位于System.ComponentModel.TypeConverter.ConvertFrom(System.ComponentModel.ITypeDescriptor上下文、System.Globalization.CultureInfo区域性、System.Object值)[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上下文,System.Globalization.CultureInfo区域性,System.Object值)[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-x64/mcs/class/referencesource/System/compmod/System/ComponentModel/TypeConverter.cs:137中的System.ComponentModel.TypeConverter.ConvertFromString(System.String文本)[0x00000]

但是我得到了另一个


我做错了什么以及如何修复它?特别是当值为null时如何处理从字符串的转换


试试这个:

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

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

如果希望在其“number”类型(int、short、long等)时返回“number”,或在其为字符串时返回
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);
}

为什么我们不在将其转换为t之前简单地检查
value
been
null
,并返回
default(t)
的硬编码结果?它应该为null吗?您可以向我们显示调用部分吗?您可以在转换值之前尝试类似的操作:
if(string.IsNullOrWhiteSpace(value)){return default(t);}
代码不显示Stacktrace,只显示[External code],因此我无法检查调用部分。我已附加了图像。如果
ConvertFromString()
仅抛出
NotSupportedException
s?这可能修复了异常,但如果您将
字符串作为
T
传递,您将得到
0
作为结果(而不是“”或
null
)。对三元语句的细微更改将得到预期结果。
返回(T)conv.ConvertFromString(string.IsNullOrWhiteSpace(value)?string.Empty:value);
返回(T)conv.ConvertFromString(string.IsNullOrWhiteSpace(value)?默认值(string):value);
return (T) conv.ConvertFromString(string.IsNullOrWhiteSpace(value) ? "0" : value);
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);
}
. . .
string value = ConfigurationManager.AppSettings[configName];
. . .