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
beennull
,并返回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];
. . .