• NET 特性(Attribute)


    特性(Attribute)是用于在运行时传递程序中各种元素(比如类、方法、结构、枚举、组件等)的行为信息的声明性标签。

    您可以通过使用特性向程序添加声明性信息。一个声明性标签是通过放置在它所应用的元素前面的方括号([ ])来描述的。

    特性(Attribute)用于添加元数据,如编译器指令和注释、描述、方法、类等其他信息。

    .Net 框架提供了两种类型的特性:预定义特性和自定义特性。

    1.自定义特性:Net 框架允许创建自定义特性,用于存储声明性的信息,且可在运行时被检索。该信息根据设计标准和应用程序需要,可与任何目标元素相关。

    创建并使用自定义特性包含四个步骤:

    • 声明自定义特性
    • 构建自定义特性
    • 在目标程序元素上应用自定义特性
    • 通过反射访问特性
        /// <summary>
        /// 别名特性
        /// </summary>
        [AttributeUsage(AttributeTargets.Class | AttributeTargets.Property, AllowMultiple = true, Inherited = true)]
        public class AliasAttribute : Attribute
        {
            /// <summary>
            /// 别名
            /// </summary>
            public string Name { get; set; }
    
            /// <summary>
            /// 别名类型
            /// </summary>
            public Type Type { get; set; }
    
            /// <summary>
            /// 别名名称,类型默认为string
            /// </summary>
            /// <param name="name"></param>
            public AliasAttribute(string name)
            {
                Name = name;
                Type = typeof(string);
            }
    
            /// <summary>
            /// 别名名称,类型为传入类型
            /// </summary>
            /// <param name="name"></param>
            public AliasAttribute(string name, Type type)
            {
                Name = name;
                Type = type;
            }
        }

    2.在目标程序元素上应用自定义特性

        public class UserDto
        {
            /// <summary>
            /// 姓名
            /// </summary>
            [Alias("name", typeof(string))]
            public string Name { get; set; }
    
            /// <summary>
            /// 电话
            /// </summary>
            [Alias("phone", typeof(string))]
            public string Phone { get; set; }
    
            /// <summary>
            /// 备注
            /// </summary>
            [Alias("remark", typeof(string))]
            public string Remark { get; set; }
        }

    3.通过反射访问特性

                //  获取type
                var userType = typeof(UserDto);
                //  获取类中所有公共属性集合
                var PropertyArr = userType.GetProperties();
                foreach (var itemProperty in PropertyArr)
                {
                    //  获取属性上存在AliasAttribute的数组
                    var customAttributesArr = itemProperty.GetCustomAttributes(typeof(AliasAttribute), true);
                    if (customAttributesArr.Any())
                    {
                        //  获取特性
                        var first = customAttributesArr.FirstOrDefault();
                    }
                    else {
                        //  不存在特性
                    }
                }
  • 相关阅读:
    js创建form添加input项目并提交表单
    DataTable 修改列名 删除列 调整列顺序
    文件夹右键出取得管理员所有权选项
    c#多线程
    js用计时器加载大量dom
    c# 将html添加进剪贴板(带格式)
    landa语法
    c# @符号后面对 双引号转义
    asp.net web api CORS
    IIS中发布网站的问题
  • 原文地址:https://www.cnblogs.com/Cailf/p/11382063.html
Copyright © 2020-2023  润新知