Net特性类Description了解下
NET特性类都有个特点类名+Attribute,继承基类Attribute,我们看下微软自带的特性类:DescriptionAttribute
namespace System.ComponentModel
{
// 摘要:
// 指定属性或事件的说明。
[AttributeUsage(AttributeTargets.All)]
public class DescriptionAttribute : Attribute
{
// 摘要:
// 指定 System.ComponentModel.DescriptionAttribute 的默认值,即空字符串 ("")。此 static 字段是只读的。
public static readonly DescriptionAttribute Default;
// 摘要:
// 不带参数初始化 System.ComponentModel.DescriptionAttribute 类的新实例。
public DescriptionAttribute();
//
// 摘要:
// 初始化 System.ComponentModel.DescriptionAttribute 类的新实例并带有说明。
//
// 参数:
// description:
// 说明文本。
[TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
public DescriptionAttribute(string description);
// 摘要:
// 获取存储在此特性中的说明。
//
// 返回结果:
// 存储在此特性中的说明。
public virtual string Description { get; }
//
// 摘要:
// 获取或设置作为说明存储的字符串。
//
// 返回结果:
// 作为说明存储的字符串。默认值为空字符串 ("")。
protected string DescriptionValue { get; set; }
// 摘要:
// 返回给定对象的值是否等于当前的 System.ComponentModel.DescriptionAttribute。
//
// 参数:
// obj:
// 要进行值的相等性测试的对象。
//
// 返回结果:
// 如果给定对象的值等于当前对象的值,则为 true;否则为 false。
public override bool Equals(object obj);
public override int GetHashCode();
//
// 摘要:
// 返回一个值,该值指示这是否为默认 System.ComponentModel.DescriptionAttribute 实例。
//
// 返回结果:
// 如果这是默认 System.ComponentModel.DescriptionAttribute 实例,则为 true;否则为 false。
public override bool IsDefaultAttribute();
}
}
看完这个类,我们了解到,此类是用来描述什么的,作用对象可以是类、属性、字段、接口、抽象、xxx
我们来看个作用对象为类的,我们定义一个人的对象,有两个属性 姓名和性别
public class Person
{
[Description("姓名")]
public string Name { get; set; }
[Description("性别")]
public int Sex { get; set; }
}
是不是觉得很熟悉,网上很多ORM工具生成的代码是不是和上面很像,但大多数都是自定义特性类,其实特性类,在我们实际做项目中起到很大的作用,本人举一个场景,在做报表的过程中,导出EXCEL过程中,列名和列的类型都是很有讲究的,一般我们准备的数据大多数都是List集合,其对象就是一个类,我们可以用到特性类来描述每列的中文名称,再读取字段类型,基本都是可以满足导出功能,下面看一个封装代码
public class ReflectionHelper<T> where T : new()
{
/// <summary>
/// 获取特性信息
/// </summary>
/// <param name="t"></param>
/// <returns></returns>
public static List<PropertityFieldInfo> GetPropertiyInfo()
{
List<PropertityFieldInfo> listRtn = new List<PropertityFieldInfo>();
Dictionary<int, string> dicPropertiy = new Dictionary<int, string>();
Dictionary<int, Type> dicFiled = new Dictionary<int, Type>();
Dictionary<int, string> dicFiledName = new Dictionary<int, string>();
T obj = new T();
;
var properties = obj.GetType().GetProperties();
var files = obj.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public);
foreach (var p in properties)
{
var v = (DescriptionAttribute[])p.GetCustomAttributes(typeof(DescriptionAttribute), false);
].Description;
dicFiledName.Add(pindex, p.Name);
dicPropertiy.Add(pindex, desc);
pindex++;
}
;
foreach (var f in files)
{
var fieldType = f.FieldType;
dicFiled.Add(findex, fieldType);
findex++;
}
foreach (KeyValuePair<int, string> kv in dicPropertiy)
{
PropertityFieldInfo m = new PropertityFieldInfo();
m.Name = dicFiledName[kv.Key];
m.Desc = dicPropertiy[kv.Key];
m.Type = dicFiled[kv.Key];
listRtn.Add(m);
}
return listRtn;
}
/// <summary>
/// 获取所有列名描述
/// </summary>
/// <returns></returns>
public static List<string> GetPropertiyDescInfo()
{
List<string> list = new List<string>();
var propertiyInfos = GetPropertiyInfo();
foreach (var item in propertiyInfos)
{
list.Add(item.Desc);
}
return list;
}
}
public class PropertityFieldInfo
{
public string Name { get; set; }
public string Desc { get; set; }
public Type Type { get; set; }
}
控制台运行下
class Program
{
static void Main(string[] args)
{
var dic = ReflectionHelper<Person>.GetPropertiyInfo();
foreach (var kv in dic)
{
Console.WriteLine(kv.Name);
Console.WriteLine(kv.Type);
Console.WriteLine(kv.Desc);
}
Console.Read();
}
}
看结果

总结:ReflectionHelper 此类用到反射和泛型机制,有兴趣小伙伴可以自学下,关于自定义特性类,本人博客中也写过,好了,希望此文对大家有帮助,喜欢的点个赞,3Q!
Net特性类Description了解下的更多相关文章
- C# 所有特性,特性所在命名空间,那些命名空间拥有特性类
文章持续补充中 特性并不是集中在某一命名空间中,而是不同的特性在不同的命名空间下,特性是某一命名空间下提供的语法糖. 有哪些命名空间提供特性: 命名空间 描述 Microsoft.Build.Fram ...
- C#特性类的使用
特性类的使用过程: 第一步:定义一个特性类,定义一些成员来包含验证时需要的数据:第二步:创建特性类实例:创建一个特性类的实例,里面包含着验证某一个属性或者字段需要的数据.将该实例关联到某个属性上面.第 ...
- paip。java 高级特性 类默认方法,匿名方法+多方法连续调用, 常量类型
paip.java 高级特性 类默认方法,匿名方法+多方法连续调用, 常量类型 作者Attilax 艾龙, EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http ...
- 巧用CSS3 :target 伪类制作Dropdown下拉菜单(无JS)
:target 是CSS3 中新增的一个伪类,用以匹配当前页面的URI中某个标志符的目标元素(比如说当前页面URL下添加#comment就会定位到id=“comment”的位置,俗称锚).CSS3 为 ...
- WPF{ComboBox绑定类对象, 下拉列显示的值,与取到的值}
DisplayMemberPath 是用来显示下拉列表的值 SelectedValuePath是用来取得选中项的值. ComboBox绑定类对象, 下拉列显示的值,与取到的值 string. Join ...
- Siki_Unity_2-1_API常用方法和类详细讲解(下)
Unity 2-1 API常用方法和类详细讲解(下) 任务101&102:射线检测 射线origin + direction:射线检测:射线是否碰撞到物体 (物体需要有碰撞器),碰撞物体的信息 ...
- 巧用CSS3:target 伪类制作Dropdown下拉菜单(无JS)
原文链接:http://devework.com/css3-target-dropdown.html :target 是CSS3 中新增的一个伪类,用以匹配当前页面的URI中某个标志符的目标元素(比如 ...
- UML类图(下):关联、聚合、组合、依赖
前言 上一篇文章UML类图(上):类.继承.实现,讲了UML类图中类.继承.实现三种关系及其在UML类图中的画法,本文将接着上文的内容,继续讲讲对象之间的其他几种关系,主要就是关联.聚合.组合.依赖, ...
- 前端笔记之ES678&Webpack&Babel(中)对象|字符串|数组的扩展&函数新特性&类
一.对象的扩展 1.1对象属性名表达式 ES6可以在JSON中使用[]包裹一个key的名字.此时这个key将用表达式作为属性名(被当做变量求值),这个key值必须是字符串. var a = 'name ...
随机推荐
- 精《Linux内核精髓:精通Linux内核必会的75个绝技》一HACK #5 使用checkpatch.pl检查补丁的格式
HACK #5 使用checkpatch.pl检查补丁的格式 本节介绍发布前检查补丁格式的方法.Linux内核是由多个开发者进行开发的.因此,为了保持补丁评估与源代码的可读性,按照统一的规则进行编写是 ...
- Git同时提交到多个远程仓库
使用git同时提交到多个远程库的操作方式为: 比如我需要你将同一份代码提交到如下的两个库中: https://gitee.com/FelixBinCloud/recruit.git https://g ...
- 如何使用C#程序给PDF文件添加编辑域
PDF文档通常是不能编辑的,但有些时候需要在PDF文档中填写日期或签名之类,就需要在PDF有能编辑的文本域,本文介绍怎样用C#来实现这一功能. 环境 工具:VS2015 语言:C# 操作PDF类库:i ...
- URL的名称设置
1. 对于login.html 此为跳转文件, 加入了参数nid,在views.py中进行关于request.POST.get()的文件中获取 <a href='/detail?nid={{k} ...
- Linux 文件管理命令语法、参数、实例全汇总(一)
命令:cat cat 命令用于连接文件并打印到标准输出设备上. 使用权限 所有使用者 语法格式 cat [-AbeEnstTuv] [--help] [--version] fileName 参数 ...
- zookeeper分布式锁和服务优化配置
转自:https://www.jianshu.com/p/02eeaee4357f?utm_campaign=maleskine&utm_content=note&utm_medium ...
- MYSQL TIMESTAMP with implicit DEFAULT value is deprecated.
TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp se ...
- ProjectLeader感悟
1 首先项目的目标清晰明确,开发的方案也必须清晰明确. 确保两点的做法,是面对面的访谈,很简单的可以打电话或者QQ或者邮件:方案要多次揣摩,设计出合理的方案.如果发以上两点做不好,你就会受到组员的鄙视 ...
- Unknown type name 'NSString'
今天看到个问题,编辑工程提示Unknown type name 'NSString',如下图 导致出现异常的原因是是因为工程中添加了ZipArchive(第三方开源解压缩库) 一般情况下出现“Unkn ...
- Laravel trait 使用心得
trait 是在PHP5.4中为了方便代码复用的一种实现方式,但目前我在看的的PHP项目中较少看的有程序员去主动使用这个实现方式,在laravel中有很多 trait 的使用,关于trait 在 la ...