我想檢查Enum中是否存在任何枚舉中通過的代碼。問題是我的枚舉定義如下使用代碼屬性。在Enum屬性中搜索
public enum TestEnum
{
None,
[Code("FMNG")]
FunctionsManagement,
[Code("INST_MAST_MGT")]
MasterInstManagement
}
[AttributeUsage(AttributeTargets.Field, AllowMultiple = false)]
public class CodeAttribute : Attribute
{
readonly string _code;
public string Code
{
get
{
return _code;
}
}
public CodeAttribute(string code)
{
_code = code;
}
}
現在,我有可用的字符串(如「FMNG」),我想找尋回枚舉該枚舉與傳遞的字符串,這將是枚舉屬性存在。
我該如何檢查/得到使用或傳遞字符串的枚舉?我嘗試過使用Enum.IsDefined(typeof(ActivityEnum), "FMNG")
,但它不適用於枚舉屬性。
您可以用'Description'屬性,而不是'Code'的'enum'? – Habib
實際上,代碼是預定義的,並且有50多個枚舉,因此我現在不能更改代碼屬性。 –