• C# 扩展方法


    实例:通过扩展方法,获取枚举的描述特性

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Reflection;
    using System.Text;
    
    namespace ConsoleApp1
    {
        public static class ExHelper
        {
            public static string GetD(this Enum enumValue)
            {
                string value = enumValue.ToString();
                FieldInfo field = enumValue.GetType().GetField(value);
                object[] objs = field.GetCustomAttributes(typeof(DescriptionAttribute), false);    //获取描述属性
                if (objs == null || objs.Length == 0)    //当描述属性没有时,直接返回名称
                    return value;
                DescriptionAttribute descriptionAttribute = (DescriptionAttribute)objs[0];
                return descriptionAttribute.Description;
            }
        }
    
        public enum MySex
        {
            [Description("")]
            male = 0,
            [Description("")]
            female = 1
        }
    }
    天生我材必有用,千金散尽还复来
  • 相关阅读:
    程序猿小白博客报道
    UML
    mysql隔离级别相关
    mac使用相关笔记
    git相关
    maven相关
    springboot相关
    guava
    IDEA高效运用技巧
    spring事务相关
  • 原文地址:https://www.cnblogs.com/ligenyun/p/9070293.html
Copyright © 2020-2023  润新知