要尽量长话短说一个枚举的描述,我创建了以下枚举搜索包含斜线
public enum Frequency
{
[Description("Monthly")]
Monthly,
[Description("Quarterly")]
Quarterly,
[Description("N/A")]
NA
}
那我也使用相同的描述字符串组合框。
当我选择一个新的选择,特别是“N/A”,它不能正确读取它。
,我使用搜索根据传递的字符串是正确的枚举代码...
/// Returns an enum of the specified type that matches the string value passed in. Note this does ignore case
<param name="value">The string value.</param>
public static TEnum GetEnum<TEnum>(string value)
{
if (string.IsNullOrEmpty(value))
{
// Default not set value name
value = "None";
}
return (TEnum)System.Enum.Parse(typeof(TEnum), value.Replace(" ", string.Empty), true);
}
所以,当值=“N/A”,我得到以下错误。 。
"An unhandled exception of type 'System.ArgumentException' occurred in mscorlib.dll"
附加信息:请求的值“N/A”未找到“
我似乎无法理解为什么这可能发生还有另外一个,预先存在的组合框在那里。解锁还包含一个'/'字符,并发生相同的错误。所以它不是我做错了,看起来,但只是枚举字符串检查的行为。
任何洞察到为什么这会导致问题将不可思议的赞赏。 :) 谢谢!
编辑: 更多信息..
因此,这是触发枚举搜索代码..
if (this.FrequencyCombo.SelectedItem != null && !this.FrequencyCombo.SelectedItem.Equals(Utilities.GetDescription(currentLoan.Frequency)))
{
currentLoan.Frequency = Utilities.GetEnum<Frequency>(this.FrequencyCombo.SelectedItem.ToString());
}
它正在检查名称(NA),而不是描述(N/A)。 – juharr 2014-11-24 15:33:59
显示的代码中没有任何内容表示完全使用描述。 – Plutonix 2014-11-24 15:35:21
为什么你没有文字使用'Description',但是你的组合的值是枚举值。 – Jamiec 2014-11-24 15:43:25