C#基礎(chǔ)學(xué)習(xí)系列之Attribute和反射詳解
前言
本文主要給大家介紹了關(guān)于C#基礎(chǔ)之Attribute和反射的相關(guān)內(nèi)容,分享出來供大家參考學(xué)習(xí),下面話不多說了,來一起看看詳細(xì)的介紹吧。
Attribute(特性)
Attribute是C#的一種語言特性,用于為各種實(shí)體(class,field,property)附加一些說明性信息, 并且可以在運(yùn)行時(shí)環(huán)境中檢索這些信息(通過反射)。
所有的Attribute必須繼承自Attribute類,按照約定,特性類的名稱帶有 Attribute 后綴。使用特性時(shí)可以包含或省略此后綴。
AttributeUsage
AttributeUsage是Attribute的Attribute,用于給自定義的Attribute加一些限定。
- AttributeTargets
- AllowMultiple
- Inherited
AttributeTargets指定你這個(gè)attribute限制用于哪類實(shí)體上,在這里,實(shí)體是指: class、method、constructor、field、property、GenericParameter或者用All,表明可用于所有實(shí)體。每個(gè)target標(biāo)記可以用|鏈接,如AttributeTargets.Class|AttributeTargets.Method表示這個(gè)attribute可用于class或者method。
下面例子表明了每種target的用法:
using System;
namespace AttTargsCS {
// This attribute is only valid on a class.
[AttributeUsage(AttributeTargets.Class)]
public class ClassTargetAttribute : Attribute {
}
// This attribute is only valid on a method.
[AttributeUsage(AttributeTargets.Method)]
public class MethodTargetAttribute : Attribute {
}
// This attribute is only valid on a constructor.
[AttributeUsage(AttributeTargets.Constructor)]
public class ConstructorTargetAttribute : Attribute {
}
// This attribute is only valid on a field.
[AttributeUsage(AttributeTargets.Field)]
public class FieldTargetAttribute : Attribute {
}
// This attribute is valid on a class or a method.
[AttributeUsage(AttributeTargets.Class|AttributeTargets.Method)]
public class ClassMethodTargetAttribute : Attribute {
}
// This attribute is valid on a generic type parameter.
[AttributeUsage(AttributeTargets.GenericParameter)]
public class GenericParameterTargetAttribute : Attribute {
}
// This attribute is valid on any target.
[AttributeUsage(AttributeTargets.All)]
public class AllTargetsAttribute : Attribute {
}
[ClassTarget]
[ClassMethodTarget]
[AllTargets]
public class TestClassAttribute {
[ConstructorTarget]
[AllTargets]
TestClassAttribute() {
}
[MethodTarget]
[ClassMethodTarget]
[AllTargets]
public void Method1() {
}
[FieldTarget]
[AllTargets]
public int myInt;
public void GenericMethod<
[GenericParameterTarget, AllTargets] T>(T x) {
}
static void Main(string[] args) {
}
}
}
AllowMultiple
AllowMultiple表明了這個(gè)attribute可否多次應(yīng)用于同一個(gè)實(shí)體,默認(rèn)為false
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
class MultiUseAttr : Attribute { }
[MultiUseAttr, MultiUseAttr]
class Class2 { }
Inherited
Inherited表明這個(gè)attribute是否可以被繼承傳遞,即子類或子類從父類繼承的成員是否帶這個(gè)attribute,默認(rèn)為true
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method |
AttributeTargets.Property | AttributeTargets.Field,
Inherited = true)]
public class InheritedAttribute : Attribute
{}
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method |
AttributeTargets.Property | AttributeTargets.Field,
Inherited = false)]
public class NotInheritedAttribute : Attribute
{}
using System;
using System.Reflection;
[InheritedAttribute]
public class BaseA
{
[InheritedAttribute]
public virtual void MethodA()
{}
}
public class DerivedA : BaseA
{
public override void MethodA()
{}
}
[NotInheritedAttribute]
public class BaseB
{
[NotInheritedAttribute]
public virtual void MethodB()
{}
}
public class DerivedB : BaseB
{
public override void MethodB()
{}
}
public class Example
{
public static void Main()
{
Type typeA = typeof(DerivedA);
Console.WriteLine("DerivedA has Inherited attribute: {0}",
typeA.GetCustomAttributes(typeof(InheritedAttribute), true).Length > 0);
MethodInfo memberA = typeA.GetMethod("MethodA");
Console.WriteLine("DerivedA.MemberA has Inherited attribute: {0}\n",
memberA.GetCustomAttributes(typeof(InheritedAttribute), true).Length > 0);
Type typeB = typeof(DerivedB);
Console.WriteLine("DerivedB has Inherited attribute: {0}",
typeB.GetCustomAttributes(typeof(InheritedAttribute), true).Length > 0);
MethodInfo memberB = typeB.GetMethod("MethodB");
Console.WriteLine("DerivedB.MemberB has Inherited attribute: {0}",
memberB.GetCustomAttributes(typeof(InheritedAttribute), true).Length > 0);
}
}
// The example displays the following output:
// DerivedA has Inherited attribute: True
// DerivedA.MemberA has Inherited attribute: True
//
// DerivedB has Inherited attribute: False
// DerivedB.MemberB has Inherited attribute: False
反射
Reflection,中文翻譯為反射,是審查元數(shù)據(jù)并收集關(guān)于它的類型信息的能力。元數(shù)據(jù)(編譯以后的最基本數(shù)據(jù)單元)就是一大堆的表,當(dāng)編譯程序集或者模塊時(shí),編譯器會(huì)創(chuàng)建一個(gè)類定義表,一個(gè)字段定義表,和一個(gè)方法定義表等。
反射是.Net中獲取運(yùn)行時(shí)類型信息的方式,.Net的應(yīng)用程序由幾個(gè)部分:‘程序集(Assembly)'、‘模塊(Module)'、‘類型(class)'組成,而反射提供一種編程的方式,讓程序員可以在程序運(yùn)行期獲得這幾個(gè)組成部分的相關(guān)信息, Assemblies contain modules. Modules contain classes. Classes contain functions.
System.reflection命名空間包含的幾個(gè)類,允許你反射(解析)這些元數(shù)據(jù)表的代碼
System.Reflection.Assembly System.Reflection.MemberInfo System.Reflection.EventInfo System.Reflection.FieldInfo System.Reflection.MethodBase System.Reflection.ConstructorInfo System.Reflection.MethodInfo System.Reflection.PropertyInfo System.Type
以下是上面幾個(gè)類的使用方法:
- 使用Assembly定義和加載程序集,加載在程序集清單中列出模塊,以及從此程序集中查找類型并創(chuàng)建該類型的實(shí)例。
- 使用Module了解包含模塊的程序集以及模塊中的類等,還可以獲取在模塊上定義的所有全局方法或其他特定的非全局方法。
- 使用ConstructorInfo了解構(gòu)造函數(shù)的名稱、參數(shù)、訪問修飾符(如pulic 或private)和實(shí)現(xiàn)詳細(xì)信息(如abstract或virtual)等。
- 使用Type的GetConstructors或 GetConstructor方法來調(diào)用特定的構(gòu)造函數(shù)。
- 使用MethodInfo了解方法的名稱、返回類型、參數(shù)、訪問修飾符(如pulic 或private)和實(shí)現(xiàn)詳細(xì)信息(如abstract或virtual)等。
- 使用Type的GetMethods或GetMethod方法來調(diào)用特定的方法。
- 使用FiedInfo了解字段的名稱、訪問修飾符(如public或private)和實(shí)現(xiàn)詳細(xì)信息(如static)等,并獲取或設(shè)置字段值。
- 使用EventInfo了解事件的名稱、事件處理程序數(shù)據(jù)類型、自定義屬性、聲明類型和反射類型等,添加或移除事件處理程序。
- 使用PropertyInfo了解屬性的名稱、數(shù)據(jù)類型、聲明類型、反射類型和只讀或可寫狀態(tài)等,獲取或設(shè)置屬性值。
- 使用ParameterInfo了解參數(shù)的名稱、數(shù)據(jù)類型、是輸入?yún)?shù)還是輸出參數(shù),以及參數(shù)在方法簽名中的位置等。
反射的作用:
- 可以使用反射動(dòng)態(tài)地創(chuàng)建類型的實(shí)例,將類型綁定到現(xiàn)有對(duì)象,或從現(xiàn)有對(duì)象中獲取類型
- 應(yīng)用程序需要在運(yùn)行時(shí)從某個(gè)特定的程序集中載入一個(gè)特定的類型,以便實(shí)現(xiàn)某個(gè)任務(wù)時(shí)可以用到反射。
使用反射獲取類型
public void Process(object processObj)
{
Type t = processsObj.GetType();
if(t.GetInterface(“ITest”) !=null)
…
}
創(chuàng)建一個(gè)對(duì)象
public class TestClass {
private string _value;
public TestClass() {
}
public TestClass(string value) {
_value = value;
}
public string GetValue( string prefix ) {
if( _value==null )
return "NULL";
else
return prefix+" : "+_value;
}
//獲取類型信息
Type t = Type.GetType("TestSpace.TestClass");
//構(gòu)造器的參數(shù)
object[] constuctParms = new object[]{"timmy"};
//根據(jù)類型創(chuàng)建對(duì)象
object dObj = Activator.CreateInstance(t,constuctParms);
//獲取方法的信息
MethodInfo method = t.GetMethod("GetValue");
//調(diào)用方法的一些標(biāo)志位,這里的含義是Public并且是實(shí)例方法,這也是默認(rèn)的值
BindingFlags flag = BindingFlags.Public | BindingFlags.Instance;
//GetValue方法的參數(shù)
object[] parameters = new object[]{"Hello"};
//調(diào)用方法,用一個(gè)object接收返回值
object returnValue = method.Invoke(dObj,flag,Type.DefaultBinder,parameters,null);
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對(duì)我們的支持。
上一篇:C#設(shè)計(jì)模式之Strategy策略模式解決007大破密碼危機(jī)問題示例
欄 目:C#教程
下一篇:C#設(shè)計(jì)模式之Mediator中介者模式解決程序員的七夕緣分問題示例
本文標(biāo)題:C#基礎(chǔ)學(xué)習(xí)系列之Attribute和反射詳解
本文地址:http://www.jygsgssxh.com/a1/C_jiaocheng/5490.html
您可能感興趣的文章
- 01-10C#影院售票系統(tǒng)畢業(yè)設(shè)計(jì)(1)
- 01-10輕松學(xué)習(xí)C#的運(yùn)算符
- 01-10輕松學(xué)習(xí)C#的基礎(chǔ)入門
- 01-10輕松學(xué)習(xí)C#的裝箱與拆箱
- 01-10輕松學(xué)習(xí)C#的預(yù)定義數(shù)據(jù)類型
- 01-10輕松學(xué)習(xí)C#的foreach迭代語句
- 01-10輕松學(xué)習(xí)C#的讀寫操作
- 01-10輕松學(xué)習(xí)C#的String類
- 01-10輕松學(xué)習(xí)C#的ArrayList類
- 01-10輕松學(xué)習(xí)C#的密封類


閱讀排行
本欄相關(guān)
- 01-10C#通過反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁無法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻播放器左下角滾動(dòng)新
- 01-10C#停止線程的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#通過重寫Panel改變邊框顏色與寬度的
- 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機(jī)閱讀
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 01-10delphi制作wav文件的方法
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-10C#中split用法實(shí)例總結(jié)
- 04-02jquery與jsp,用jquery
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子


