Your Ad Here
首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 编程语言 > .NET > C#.NET > Reflection & Attributes
【标  题】:Reflection & Attributes
【关键字】:Reflection,Attributes
【来  源】:http://blog.csdn.net/lucky2all/archive/2007/04/19/1571421.aspx

Reflection & Attributes

Your Ad Here 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 
 
多版本软件构建策略分析:【上一篇】
MFC深入浅出学习:【下一篇】
【相关文章】
  • 如何使CheckBoxList的Attributes属性生效
  • (Reflection)认识反射(反射实例化,反射评价,在工厂三层架构应用)
  • java反射reflection需要注意的几点
  • Execute a function in any Win32 DLL - Reflection in Win32 DLL?
  • WPF/E - 使用 Transforms 和 Opacity Masks 创建 Reflection 反射效果
  • 用 System.Reflection.Emit 来自动生成调用存储过程的实现
  • 设置窗口透明度的SetLayeredWindowAttributes函数
  • Reflection学习笔记(简单的ORM映射框架)
  • 从Atlas到Microsoft ASP.NET AJAX(3) - Class and Type Definition, Reflection APIs
  • 使用反射(reflection)的简单例子
  • 【随机文章】
  • DNS在活动目录中的使用常见问题及解答
  • Struts标签+javascript的省市连动
  • VB编写行长压缩程序
  • Windows Server 2003全接触
  • 日期与时间函数库
  • wince 通话记录的自动删除实现
  • 2004-12-21的20个小时 (二)
  • Enum:Java枚举是类类型
  • Informix存储过程示例 二
  • 源码:用perl写的基于postgresql的考勤数据管理软件
  • 【相关评论】
    没有相关评论
    【发表评论】
    姓名:
    邮件:
    随机码*
    评论*
          
    |  首 页  |  版权声明  |  联系我们   |  网站地图  |
    CopyRight © 2004-2007 bbb软讯网络 All Rigths Reserved.