【发布时间】: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);
但是,当 value 为 null 时,代码在 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);
当value为null时修复之前的异常:
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
但我得到了另一个。
我做错了什么以及如何解决?特别是当value为null时如何处理从字符串的转换?
【问题讨论】:
-
为什么我们不简单地检查
value是null之前将其转换为 T 并返回default(T)的硬编码结果?它应该是 null 吗? -
你能告诉我们调用部分吗?
-
你可以在转换价值之前尝试这样的事情:
if(string.IsNullOrWhiteSpace(value)) { return default(T); } -
代码没有显示Stacktrace,只是[External Code],所以无法查看调用部分。我已附上图片。
-
如果
ConvertFromString()只抛出NotSupportedExceptions,它如何抛出IndexOutOfRangeException? MSDN Link
标签: c# .net exception mono indexoutofboundsexception