C#特性Attribute学习
起初一直纠结于如何调用特性附着在下面那个成员的值,后来发现不需要调用,通过反射加载的时候是自动绑定上去的,即 获得成员对象之后,有一个方法可以获得特性标签。
其实从类库提供者,和类库使用者的角度,分开来看就很容易理解了。
类库提供者(MyClassLib类):
MyClassLib类有一个ShowString(object obj, string name)方法,可以打印出string信息。
并且提供一个StringLenAttribute特性,控制字符串长度。
类库使用者:
使用这个名为MyClassLib的类,来打印信息
下面看例子
使用者:
public class User
{
[StringLen()]
string myName; [StringLen()]
string myDetail; public void Go()
{
myName = "Tim Cook";
myDetail = "Apple company CEO"; MyClassLib cl = new MyClassLib();
cl.ShowString(this, myName);
cl.ShowString(this, myDetail);
//Output:
//字符串过长! 打印失败
//Apple company CEO
}
}
Use
[StringLen(xx)]
这个是自定义的特性
来看看提供者方面的代码
提供者:
定义自定义特性:
[AttributeUsage(AttributeTargets.Field, Inherited = false, AllowMultiple = true)]
public sealed class StringLenAttribute : Attribute
{
public int Len { get; set; } public StringLenAttribute(int len)
{
this.Len = len;
}
}
然后是打印字符串这个方法,和应用特性:
public class MyClassLib
{
public void ShowString(object obj, string name)
{
var atts = obj.GetType().GetFields(BindingFlags.Instance|BindingFlags.NonPublic); foreach (var item in atts)
{
var tmp = item.GetCustomAttributes(typeof(StringLenAttribute), false);
string tmp2 = string.Empty; if (tmp.Length > && (string)item.GetValue(obj) == name)
{
var stringLenAtt = (StringLenAttribute)tmp[]; if (name.Length > stringLenAtt.Len)
{
Console.WriteLine("字符串过长! 打印失败");
return;
}
}
} Console.WriteLine(name);
}
}
Provider
反射调用的话,应该是必须得到对象才能使用里面成员,所以要传入2个参数,真正编写的时候肯定涉及自己的结构类,所以这个问题并不明显。如果以后找到解决方法,会更新这篇文章。
C#特性Attribute学习的更多相关文章
- 代码走查25条疑问 C# 跳转新的标签页 C#线程处理 .Net 特性 attribute 学习 ----自定义特性 看懂 ,学会 .NET 事件的正确姿势-简单版
代码走查25条疑问 代码走查(Code Review) 是一个开发人员与架构师集中讨论代码的过程.通过代码走查可以提高代码的 质量,同时减少Bug出现的几率.但是在小公司中并没有代码走查的过程在这 ...
- .Net 特性 attribute 学习 ----自定义特性
什么是特性? [Obsolete("不要用无参构造函数",true)] 放在方式上, 该方法就不能使用了 [Serializable]放在类上面.该类就是可以序列化和反序列化使用 ...
- C# Attribute学习
由于项目中需要使用到序列化相关的技术,从而想到是否可以使用C#中的特性,特此花了近两小时学习了一下. 对于特性的学习,主要参考了两篇博文,特此感谢.以下附链接: http://www.cnblogs. ...
- .Net内置特性Attribute介绍
特性Attribute概述 特性(Attribute)是一种特殊的类型,可以加载到程序集或者程序集的类型上,这些类型包括模块.类.接口.结构.构造函数.方法.字段等,加载了特性的类型称之为特性的目标. ...
- 理解特性attribute 和 属性property的区别 及相关DOM操作总结
查一下英语单词解释,两个都可以表示属性.但attribute倾向于解释为特质,而property倾向于解释私有的.这个property的私有解释可以更方便我们下面的理解. 第一部分:区别点 第一点: ...
- C#中的特性 (Attribute) 入门 (二)
C#中的特性 (Attribute) 入门 (二) 接下来我们要自己定义我们自己的特性,通过我们自己定义的特性来描述我们的代码. 自定义特性 所有的自定义特性都应该继承或者间接的继承自Attribut ...
- C#中的特性 (Attribute) 入门 (一)
C#中的特性 (Attribute) 入门 (一) 饮水思源 http://www.cnblogs.com/Wind-Eagle/archive/2008/12/10/1351746.html htt ...
- [C#] 剖析 AssemblyInfo.cs - 了解常用的特性 Attribute
剖析 AssemblyInfo.cs - 了解常用的特性 Attribute [博主]反骨仔 [原文]http://www.cnblogs.com/liqingwen/p/5944391.html 序 ...
- [C#] C# 知识回顾 - 特性 Attribute
C# 知识回顾 - 特性 Attribute [博主]反骨仔 [原文地址]http://www.cnblogs.com/liqingwen/p/5911289.html 目录 特性简介 使用特性 特性 ...
随机推荐
- hdu1874 畅通project续(求最短路径)
畅通project续 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- ONVIFclient搜索设备获取rtsp地址开发笔记(精华篇)
概要: 眼下ONVIF协议家族设备已占领数字监控行业半壁江山以上.亲,作为开发人员的你还在犹豫是否了解下吗?本文介绍了ONVIFclient从设备搜索,鉴权,能力获取,媒体信息获取 ...
- Windows安装MySQL解压版
1:解压 2:设置环境变量 3:修改my.ini [mysqld] basedir = D:\MySQL\Server\mysql--win32 datadir = D:\MySQL\Server\d ...
- No value for key [org.hibernate.impl.SessionFactoryImpl 异常解决
使用Hibernate+Spring进行CRUD操作时.出现例如以下类似异常信息: java.lang.IllegalStateException: No value for key [org.hib ...
- “error: command 'x86_64-linux-gnu-gcc' failed with exit status 1” in virtualenv
Most of the time these are dependency-issues. Following the stack-trace of the gcc compiler one ca ...
- How to hide the create button dynamical tree view in openerp ?
<tree create="false" edit="false" > <tree attrs="{'create':[(" ...
- SettingsHBuilder
迁移时间:2017年5月20日10:56:50CreateTime--2016年9月27日14:22:26Author:Marydon1.修改HBuilder开发工具快捷键工具-->首选项- ...
- 〖Linux〗build sqlite3 for Arm
Version: sqlite-autoconf-3080100.tar.gz Download: https://www.sqlite.org/download.html 1. toolchains ...
- ribbon区域亲和配置一例
只需在springboot的配置文件中添加以下内容即可: eureka.instance.metadata-map.zone=left 该配置是一个eureka客户端的配置,并且该客户端使用了ribb ...
- Loadrunner脚本编程(2)-VuGen脚本文件的开发过程
http://www.360doc.com/content/10/0806/13/1698198_44076570.shtml 1.定义测试项目的目标,环境,脚本,测试数据,硬件等.脚本应该符合编码规 ...