NET 特性(Attribute)

转自 博客园(Fish)

特性(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 {
// 不存在特性
}
}

NET 特性(Attribute)的更多相关文章

  1. [C#] 剖析 AssemblyInfo.cs - 了解常用的特性 Attribute

    剖析 AssemblyInfo.cs - 了解常用的特性 Attribute [博主]反骨仔 [原文]http://www.cnblogs.com/liqingwen/p/5944391.html 序 ...

  2. [C#] C# 知识回顾 - 特性 Attribute

    C# 知识回顾 - 特性 Attribute [博主]反骨仔 [原文地址]http://www.cnblogs.com/liqingwen/p/5911289.html 目录 特性简介 使用特性 特性 ...

  3. C# 知识特性 Attribute

    C#知识--获取特性 Attribute 特性提供功能强大的方法,用以将元数据或声明信息与代码(程序集.类型.方法.属性等)相关联.特性与程序实体关联后,可在运行时使用"反射"查询 ...

  4. 区分元素特性attribute和对象属性property

    × 目录 [1]定义 [2]共有 [3]例外[4]特殊[5]自定义[6]混淆[7]总结 前面的话 其实attribute和property两个单词,翻译出来都是属性,但是<javascript高 ...

  5. .Net内置特性Attribute介绍

    特性Attribute概述 特性(Attribute)是一种特殊的类型,可以加载到程序集或者程序集的类型上,这些类型包括模块.类.接口.结构.构造函数.方法.字段等,加载了特性的类型称之为特性的目标. ...

  6. 【点滴积累】通过特性(Attribute)为枚举添加更多的信息

    转:http://www.cnblogs.com/IPrograming/archive/2013/05/26/Enum_DescriptionAttribute.html [点滴积累]通过特性(At ...

  7. 理解特性attribute 和 属性property的区别 及相关DOM操作总结

    查一下英语单词解释,两个都可以表示属性.但attribute倾向于解释为特质,而property倾向于解释私有的.这个property的私有解释可以更方便我们下面的理解. 第一部分:区别点 第一点:  ...

  8. 如何获取类或属性的自定义特性(Attribute)

    如何获取类或属性的自定义特性(Attribute) 问题说明: 在ActiveRecord或者其他的ORM等代码中, 我们经常可以看到自定义特性(Attribute)的存在(如下面的代码所示) [Pr ...

  9. C# 知识特性 Attribute,XMLSerialize,

    C#知识--获取特性 Attribute 特性提供功能强大的方法,用以将元数据或声明信息与代码(程序集.类型.方法.属性等)相关联.特性与程序实体关联后,可在运行时使用“反射”查询特性,获取特性集合方 ...

  10. c#特性attribute:

    特性是被编译到metadata中,  是提供给反射用的. 特性attribute:1 什么是attribute,和注释有什么区别 2 声明和使用attribute3 使用attribute完成扩展4 ...

随机推荐

  1. asp.net core 新建area使用asp-action,asp-controller不管用

    解决方法: 在新建的Area目录下,这里使用Admin,Admin/Views下新建_ViewImports.cshtml和_ViewStart.cshtml两个视图文件,复制项目自动生成的到对应的新 ...

  2. Implement Dependent Reference Properties实现依赖引用属性 (EF)

    In this lesson, you will learn how to implement properties whose values can depend on other properti ...

  3. 查找字段的筛选-使用addCustomView

    关注本人微信和易信公众号: 微软动态CRM专家罗勇 ,回复231或者20161031可方便获取本文,同时可以在第一间得到我发布的最新的博文信息,follow me!我的网站是 www.luoyong. ...

  4. 【Android】Handler消息机制

    Handler消息机制主要涉及Looper.Handler.MessageQueue.Message.其中,Looper主要负责获取消息,Handler负责发送消息及处理消息,MessageQueue ...

  5. SSH免密码登录和Git免密操作

    SSH免密码登录和Git免密操作 每次打完包后都需要把包传到对应的服务器上从而让测试人员下载安装,但是每次ssh或scp时都需要重新输入密码:使用git代码托管平台只要修改了密码就需要输入密码.本文主 ...

  6. [日常] 解决docker拉取镜像速度慢的问题

    将docker修改为国内镜像源 在/etc/docker/daemon.json文件中添加下面参数 此处使用的是中国科技大学的docker镜像源 {    "registry-mirrors ...

  7. 修改Tooltip 文字提示 的背景色 箭头颜色

    3==>vue 鼠标右击<div @contextmenu.prevent="mouseRightClick">prevent是阻止鼠标的默认事件 4==> ...

  8. logistic 回归(线性和非线性)

    一:线性logistic 回归 代码如下: import numpy as np import pandas as pd import matplotlib.pyplot as plt import ...

  9. 2、zabbix3.4的安装

    系统版本:centos7 数据库版本:mysql二进制安装5.7 zabbix:阿里云安装3.4 一.Zabbix的安装 Zabbix3.4版本官方安装手册链接:https://www.zabbix. ...

  10. 面向对象程序设计(JAVA) 第10周学习指导及要求

    2019面向对象程序设计(Java)第10周学习指导及要求 (2019.11.1-2019.11.4)   学习目标 1.掌握java异常处理技术: 2.了解断言的用法: 3.了解日志的用途: 4.掌 ...