惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
腾讯CDC
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
L
LINUX DO - 热门话题
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Project Zero
Project Zero
V
Vulnerabilities – Threatpost
Cisco Talos Blog
Cisco Talos Blog
P
Palo Alto Networks Blog
C
Cisco Blogs
A
Arctic Wolf
月光博客
月光博客
The GitHub Blog
The GitHub Blog
T
The Blog of Author Tim Ferriss
量子位
小众软件
小众软件
Latest news
Latest news
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Microsoft Security Blog
Microsoft Security Blog
T
The Exploit Database - CXSecurity.com
Security Latest
Security Latest
N
Netflix TechBlog - Medium
K
Kaspersky official blog
人人都是产品经理
人人都是产品经理
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园_首页
Y
Y Combinator Blog
P
Proofpoint News Feed
H
Hackread – Cybersecurity News, Data Breaches, AI and More
M
MIT News - Artificial intelligence
T
Threat Research - Cisco Blogs
S
Schneier on Security
D
Docker
Scott Helme
Scott Helme
MyScale Blog
MyScale Blog
Spread Privacy
Spread Privacy
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
GbyAI
GbyAI
有赞技术团队
有赞技术团队
Google DeepMind News
Google DeepMind News
The Hacker News
The Hacker News
H
Help Net Security
Simon Willison's Weblog
Simon Willison's Weblog
J
Java Code Geeks
C
Cyber Attacks, Cyber Crime and Cyber Security
T
Tenable Blog
B
Blog
Know Your Adversary
Know Your Adversary
IT之家
IT之家

博客园 - 木乃伊人

VS Code 搭建LangChain开发环境 VSCode Unable to import XXX 问题解决记录 大模型应用开发-聊天机器人-保存聊天记录 try-catch中的throw和throw ex的区别 TDD Google Chrome 默认非安全端口列表 EF Core的预先加载、延迟加载、实体追踪 同步、异步、回调 软件设计原则 UML类图 Seq Serilog 多线程整理 Vuex和Pinia Vue3+TS+Vite+pinia Vite创建Vue3项目 闭包 IdentitySrever4 ElasticSearch 前端防止重复提交案例
枚举
木乃伊人 · 2024-04-03 · via 博客园 - 木乃伊人
// 枚举
public enum enumStudent
{
    [Description("性别")]
    sex = 0,
    [Description("年龄")]
    age = 1,
}
 
// 获取方法
public string GetDescriptionByEnum(Enum enumValue)
{
    string value = enumValue.ToString();
    System.Reflection.FieldInfo field = enumValue.GetType().GetField(value);
    object[] objs = field.GetCustomAttributes(typeof(DescriptionAttribute), false);    //获取描述属性
    if (objs.Length == 0)    //当描述属性没有时,直接返回名称
        return value;
    DescriptionAttribute descriptionAttribute = (DescriptionAttribute)objs[0];
    return descriptionAttribute.Description;
}
 
 
// 调用示例
GetDescriptionByEnum(enumStudent.age) → 年龄
 
 
public T GetEnumByDescription<T>(string description) where T : Enum
{
    System.Reflection.FieldInfo[] fields = typeof(T).GetFields();
    foreach(System.Reflection.FieldInfo field in fields)
    {
        object[] objs = field.GetCustomAttributes(typeof(DescriptionAttribute), false);    //获取描述属性
        if (objs.Length > 0 && (objs[0] as DescriptionAttribute).Description == description)
        {
            return (T)field.GetValue(null);
        }
    }
 
    throw new ArgumentException(string.Format("{0} 未能找到对应的枚举.", description), "Description");
}
 
// 调用示例