软讯网络 > 编程语言 > .NET > C#.NET > Reflection & Attributes
【标 题】:Reflection & Attributes
【关键字】:
Reflection,Attributes
【来 源】:http://blog.csdn.net/lucky2all/archive/2007/04/19/1571421.aspx
Reflection & Attributes
Reflection & Attributes lucky2all@gmail.com 2007-04-19 最直接,到位的描述 [from MSDN] Attributes provide a powerful method of associating declarative information with C# code (types, methods, properties, and so forth). Once associated with a program entity, the attribute can be queried at run time using a technique called Reflection. attributes 有两种, CLR基础类库已定义和自己定义。 应用与实现 (1) 已定义 using System; using System.IO; using System.Runtime.Serialization.Formatters.Soap ; // //class SerializableAttribute:Attribute //{ //} [Serializable] public class Point { public int x; public int y; public Point(int x, int y) { this.x=x; this.y=y; } } class App { public static void Main() { Serialize(); } public static void DeSerialize() { SoapFormatter sf=new SoapFormatter (); FileStream fs = new FileStream("Data.XML", FileMode.Open); object obj=sf.Deserialize(fs); Point p=(Point)obj; Console.WriteLine(p.x); Console.WriteLine(p.y); fs.Close(); } public static void Serialize() { Point p1=new Point(100,200); SoapFormatter sf=new SoapFormatter (); FileStream fs = new FileStream("Data.XML", FileMode.Create); sf.Serialize(fs, p1); fs.Close(); } } // output <SOAP-ENV:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:clr="http://schemas.microsoft.com/soap/encoding/clr/1.0" SOAP- ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> <SOAP-ENV:Body> <a1:Point id="ref-1" xmlns:a1="http://schemas.microsoft.com/clr/assem/attribute2%2C%20Version%3D0.0.0.0%2C%20Culture% 3Dneutral%2C%20PublicKeyToken%3Dnull"> <x>100</x> <y>200</y> </a1:Point> </SOAP-ENV:Body> </SOAP-ENV:Envelope> (2) 自定义属性及反射获取 [refer MSDN] using System; //A角色:定义特性类 public class AuthorAttribute:Attribute { string firstName; string lastName; public AuthorAttribute(string firstName, string lastName) { this.firstName=firstName; this.lastName=lastName; } public string FirstName { get { return this.firstName; } set { this.firstName=value; } } public string LastName { get { return this.lastName; } set { this.lastName=value; } } } //A角色:提供一组服务来表明 “使用了AuthorAttribute的类将具有什么功能” class Utility { public static void Process(object obj) { Type t=obj.GetType(); Attribute att=Attribute.GetCustomAttribute(t, typeof(AuthorAttribute)); // 这里用到了反射(Reflection) if(att!=null) { AuthorAttribute author=(AuthorAttribute)att; Console.WriteLine(author.FirstName); Console.WriteLine(author.LastName); } } } //B 角色:使用特性类--标记Point具有Author特性, (系统会自动添加后缀Attribute, 查找AuthorAttribute定义) [Author("Bjarne","Stroupstup")] class Point { public int x; public int y; } //B 角色: class App { public static void Main() { Utility.Process(new Point()); } } /////////////////////////////////////////////// // output Bjarne Stroupstup