要儘量長話短說一個枚舉的描述,我創建了以下枚舉搜索包含斜線
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