小Q的博客

  • 首页
  • net编程
    • 产品和框架
    • 开发实例
    • 经验技巧
    • 开源组件
  • wp独立站
  • 自媒体
  • 日记本
  • 工具箱
每个程序员,都应该有一个自己的博客站
  1. 首页
  2. net编程
  3. 经验技巧
  4. 正文

net中c#如何获取字段、属性、方法的自定义特性?

2022年12月20日 295点热度 0人点赞 0条评论

在C#中,自定义特性的创建用于将声明信息与代码(程序集、类型、方法、属性等)相关联,以任何需要的方式。特性增强了.NET的可扩展性能。

目录

  • 1、创建特性步骤
  • 2、读取特性代码
  • 3、调用示例

1、创建特性步骤

1.1、定义一个自定义特性类,此特性类继承自System.Attribute类
1.2、自定义特性类的命名以Attribute为后缀
1.3、使用AttributeUsage特性来指定已创建的自定义特性类的使用范围
1.4、创建自定义特性的构造函数和可访问属性

public class MustInputValidAttribute : System.Attribute
{
    /// <summary>
    /// 是否必输
    /// </summary>
    public bool IsMustInput { get; set; }

    /// <summary>
    /// 提示信息
    /// </summary>
    public string Message { get; set; }


    /// <summary>
    /// 构造函数
    /// </summary>
    public MustInputValidAttribute()
    {
    }
    /// <summary>
    /// 构造函数
    /// </summary>
    /// <param name="isMustInput">是否必输</param>
    /// <param name="message">提示信息</param>
    public MustInputValidAttribute(bool isMustInput, string message)
    {
        this.IsMustInput = isMustInput;
        this.Message = message;
    }
}

 

2、读取特性代码

public class AttributeHelper
{
    /// <summary>
    /// 获取成员变量对象中指定自定义特性名称的对象
    /// </summary>
    /// <param name="field">成员对象</param>
    /// <param name="attributeName">自定义特性名称</param>
    /// <returns>自定义特性对象</returns>
    public static object GetCustomAttribute(FieldInfo field, string attributeName)
    {           
        //参数检测
        if (field == null) throw new ArgumentNullException("GetCustomAttribute.field");
        if (string.IsNullOrEmpty(attributeName)) throw new ArgumentNullException("GetCustomAttribute.attributeName");

        //获取属性对象所有自定义特性
        object attribute = null;
        object[] attributes = field.GetCustomAttributes(true);
        if (attributes.Length <= 0) return attribute;

        foreach (object obj in attributes)
        {
            if (obj.GetType().Name == attributeName)
            {
                attribute = obj;
                break;
            }
        }

        return attribute;
    }

    /// <summary>
    /// 获取属性对象中指定自定义特性名称的对象
    /// </summary>
    /// <param name="pi">属性对象</param>
    /// <param name="attributeName">自定义特性名称</param>
    /// <returns>自定义特性对象</returns>
    public static object GetCustomAttribute(PropertyInfo pi, string attributeName)
    {
        //参数检测
        if (pi == null) throw new ArgumentNullException("GetCustomAttribute.pi");
        if (string.IsNullOrEmpty(attributeName)) throw new ArgumentNullException("GetCustomAttribute.attributeName");

        //获取属性对象所有自定义特性
        object attribute = null;
        object[] attributes = pi.GetCustomAttributes(true);
        if (attributes.Length <= 0) return attribute;

        foreach (object obj in attributes)
        {
            if (obj.GetType().Name == attributeName)
            {
                attribute = obj;
                break;
            }
        }

        return attribute;
    }

    /// <summary>
    /// 获取方法对象中指定自定义特性名称的对象
    /// </summary>
    /// <param name="pi">方法对象</param>
    /// <param name="attributeName">自定义特性名称</param>
    /// <returns>自定义特性对象</returns>
    public static object GetCustomAttribute(MethodInfo mi, string attributeName)
    {
        //参数检测
        if (mi == null) throw new ArgumentNullException("GetCustomAttribute.mi");
        if (string.IsNullOrEmpty(attributeName)) throw new ArgumentNullException("GetCustomAttribute.attributeName");            

        //获取属性对象所有自定义特性
        object attribute = null;
        object[] attributes = mi.GetCustomAttributes(true);
        if (attributes.Length <= 0) return attribute;

        foreach (object obj in attributes)
        {
            if (obj.GetType().Name == attributeName)
            {
                attribute = obj;
                break;
            }
        }

        return attribute;
    }

    /// <summary>
    /// 获取方法对象中指定自定义特性名称的对象
    /// </summary>
    /// <param name="pi">方法对象</param>
    /// <param name="attributeName">自定义特性名称</param>
    /// <returns>自定义特性对象</returns>
    public static object GetCustomAttribute(MethodBase mi, string attributeName)
    {      
        //参数检测
        if (mi == null) throw new ArgumentNullException("GetCustomAttribute.mi");
        if (string.IsNullOrEmpty(attributeName)) throw new ArgumentNullException("GetCustomAttribute.attributeName"); 

        //获取属性对象所有自定义特性
        object attribute = null;
        object[] attributes = mi.GetCustomAttributes(true);
        if (attributes.Length <= 0) return attribute;

        foreach (object obj in attributes)
        {
            if (obj.GetType().Name == attributeName)
            {
                attribute = obj;
                break;
            }
        }

        return attribute;
    }

    /// <summary>
    /// 获取事件对象中指定自定义特性名称的对象
    /// </summary>
    /// <param name="ei">事件对象</param>
    /// <param name="attributeName">自定义特性名称</param>
    /// <returns>自定义特性对象</returns>
    public static object GetCustomAttribute(EventInfo ei, string attributeName)
    {          
        //参数检测
        if (ei == null) throw new ArgumentNullException("GetCustomAttribute.ei");
        if (string.IsNullOrEmpty(attributeName)) throw new ArgumentNullException("GetCustomAttribute.attributeName"); 

        //获取属性对象所有自定义特性
        object attribute = null;
        object[] attributes = ei.GetCustomAttributes(true);
        if (attributes.Length <= 0) return attribute;

        foreach (object obj in attributes)
        {
            if (obj.GetType().Name == attributeName)
            {
                attribute = obj;
                break;
            }
        }

        return attribute;
    }
}

 

3、调用示例

attributeTemp = null;
attributeTemp = AttributeHelper.GetCustomAttribute(pi, "MustInputValidAttribute");

 

相关阅读

net中c#使用ExcelMapper组件,高效读写Excel文件

c#中实现AOP的N种解决方案,ILEmit的最简单

标签: c#教程 net教程 自定义特性
最后更新:2022年12月20日

小Q

80后中年不油腻大叔,喜欢编写代码、打羽毛球、做木制玩具。目前定居浙江杭州

打赏 点赞
< 上一篇
下一篇 >

文章评论

razz evil exclaim smile redface biggrin eek confused idea lol mad twisted rolleyes wink cool arrow neutral cry mrgreen drooling persevering
取消回复

COPYRIGHT © 2022 小Q的博客. ALL RIGHTS RESERVED.

Theme Kratos Made By Seaton Jiang

浙ICP备2022019157号-2