可能重复:
通过其描述属性查找枚举值
我有一个通用的扩展方法,它Description从以下方式获取属性Enum:
enum Animal
{
[Description("")]
NotSet = 0,
[Description("Giant Panda")]
GiantPanda = 1,
[Description("Lesser Spotted Anteater")]
LesserSpottedAnteater = 2
}
public static string GetDescription(this Enum value)
{
FieldInfo field = value.GetType().GetField(value.ToString());
DescriptionAttribute attribute
= Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute))
as DescriptionAttribute;
return attribute == null ? value.ToString() : attribute.Description;
}
Run Code Online (Sandbox Code Playgroud)
所以我可以......
string myAnimal = Animal.GiantPanda.GetDescription(); // = "Giant Panda"
Run Code Online (Sandbox Code Playgroud)
现在,我正试图在另一个方向上找出等效函数,比如......
Animal a = (Animal)Enum.GetValueFromDescription("Giant Panda", typeof(Animal));
Run Code Online (Sandbox Code Playgroud) 我想在我的枚举上设置空间.这是我的代码示例:
public enum category
{
goodBoy=1,
BadBoy
}
Run Code Online (Sandbox Code Playgroud)
我想设置
public enum category
{
Good Boy=1,
Bad Boy
}
Run Code Online (Sandbox Code Playgroud)
当我检索时,我想从枚举中看到Good Boy的结果
我有以下类domain和Dto类:
public class Profile
{
public string Name { get; set; }
public string SchoolGrade { get; set; }
}
public class ProfileDTO
{
public string Name { get; set; }
public SchoolGradeDTO SchoolGrade { get; set; }
}
public enum SchoolGradeDTO
{
[Display(Name = "Level One"]
LevelOne,
[Display(Name = "Level Two"]
LevelTwo,
}
Run Code Online (Sandbox Code Playgroud)
我使用以下方法:
Mapper.CreateMap<Profile, ProfileDTO>()
.ForMember(d => d.SchoolGrade , op => op.MapFrom(o => o.SchoolGrade))
Run Code Online (Sandbox Code Playgroud)
之后,我收到以下错误:
未找到请求的值"二级".
如何正确映射?
HI
,我有以下枚举
public enum Priority : byte
{
A=1,
B+ = 2,
B=4,
C=8,
D=16,
E=32
}
Run Code Online (Sandbox Code Playgroud)
我想添加B+枚举,但它给了我错误