惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - 【当耐特】
Help Net Security
Help Net Security
P
Proofpoint News Feed
J
Java Code Geeks
爱范儿
爱范儿
Last Week in AI
Last Week in AI
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
F
Full Disclosure
Google DeepMind News
Google DeepMind News
H
Help Net Security
G
Google Developers Blog
Jina AI
Jina AI
Vercel News
Vercel News
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
L
Lohrmann on Cybersecurity
S
Schneier on Security
Microsoft Azure Blog
Microsoft Azure Blog
IT之家
IT之家
Security Archives - TechRepublic
Security Archives - TechRepublic
阮一峰的网络日志
阮一峰的网络日志
N
News and Events Feed by Topic
GbyAI
GbyAI
B
Blog
O
OpenAI News
博客园_首页
Cisco Talos Blog
Cisco Talos Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Hacker News: Ask HN
Hacker News: Ask HN
TaoSecurity Blog
TaoSecurity Blog
腾讯CDC
MongoDB | Blog
MongoDB | Blog
M
MIT News - Artificial intelligence
C
Cybersecurity and Infrastructure Security Agency CISA
Cyberwarzone
Cyberwarzone
Webroot Blog
Webroot Blog
Simon Willison's Weblog
Simon Willison's Weblog
Y
Y Combinator Blog
C
Cisco Blogs
A
Arctic Wolf
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
T
The Exploit Database - CXSecurity.com
Security Latest
Security Latest
AI
AI
W
WeLiveSecurity
aimingoo的专栏
aimingoo的专栏
The Register - Security
The Register - Security
Project Zero
Project Zero
H
Hackread – Cybersecurity News, Data Breaches, AI and More
N
Netflix TechBlog - Medium
Blog — PlanetScale
Blog — PlanetScale

博客园 - Niyo Wong

用于主题检测的临时日志(f89708b9-f679-4391-af74-c92d9138aed4 - 3bfe001a-32de-4114-a6b4-4005b770f6d7) 测试用Word 2007发布博客 【转】ASP.NET Page事件的执行顺序 ASP.NET页面请求过程 JSP 执行过程 SQL Server 2005 错误处理笔记 .Net内存分配笔记 UML入门 与健康有约——送给每一个关心自己身体的人 VC++(MFC)数据库程序——入门 T-SQL编程基础-游标 .net中的4种事务总结 T-SQL编程基础-基本语法 Javascript调用服务器端事件 模式窗口页面不更新的问题 ASP.NET菜鸟进阶-Response.Write与RegisterXXX ASP.NET菜鸟进阶-页面间参数传递 差点把这好地儿给荒废了 我们的生活离不开笑
.net 如何设置和检索特性信息(attribute)
Niyo Wong · 2008-04-14 · via 博客园 - Niyo Wong

  1.        创建一个System.Attribute的子类

[AttributeUsage(    AttributeTargets.Class | AttributeTargets.Method,    //指定可应用于那些类型,如.Class, .Method, All等
AllowMultipe = true,    //允许应用多次
Inherited = false        //不继承到派生类
)]
public class SampleAttribute : System.Attribute
{
    
private string descr;
    
private string author;
    
private string title;
    
public string Descr
{
    
get {return descr;}
    
set {descr = value;}
}

public string Author
{
    
get {return author ;}
    
set {author = value ;}
}

public string Title
{
    
get {return title ;}
    
set {title = value ;}
}

//构造方法
Public SampleAttribute (string description)
{
    descr 
= description;
}

}

在创建子类时,可以为该类标注上AttributeUsage特性. 同时指定新定义的自定义特性可以应用于哪些类型. 例如, 如果像下面这样给一个属性(property)添加刚建立的自定义特性, 编译器就会抛出异常, 提示”SampleAttribute对本声明类型无效,只对声明类和方法有效

[SampleAttribute ("description for sample attribute", Author = "Test property")]
public string SomeProerty
{
    
get {return "ok"}
}

2.        使用定义好的自定义特性

[Sample ("description for sample attribute", Author = "Tester")]
public class TestApp
{
public void Test ()
{
        Console.WriteLine (
"Author {0}, Descr {1}", author, descr);
}

}

 

Sample(…)SampleAttribute(…)的省略写法. 该代码会命名编译器调用自定义特性对象的构造函数, 并传入字符串的值(“description for sample attribute”由构造函数初始化), 然后再将”Author”属性设置为”Tester”(Author = “Tester”是由属性赋值)来完成一个特性对象的创建. 当然还可以设置其他的属性的值, 例如:

[Sample (“description for sample attribute”, Author = “Tester”, Title = “Title for this”, Descr = “cover with old descr” )]

 
3.        利用反射检索特性信息

public static void Main(string[] args)
{
        MemberInfo info 
= typeof(TestApp); //TestApp是Test()所在的类名
        SampleAttribute sample = (SampleAttribute)Attribute.GetCustomAttribute(info, typeof(SampleAttribute));
        
if(sample != null)
{
    Console.WriteLine(
"Author: {0}, Descr: {1} ", sample.Author, sample.Descr);    
    Console.ReadLine();
}

}

 完整代码下载