亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

C#基礎(chǔ)學(xué)習(xí)系列之Attribute和反射詳解

 更新時間:2017年09月10日 14:54:28   作者:Boyd Wang  
大家在使用Attribute的時候大多需要用到反射,所以放在一起。下面這篇文章主要給大家介紹了關(guān)于C#基礎(chǔ)學(xué)習(xí)系列之Attribute和反射的相關(guān)資料,文中給出了詳細的示例代碼供大家參考學(xué)習(xí),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。

前言

本文主要給大家介紹了關(guān)于C#基礎(chǔ)之Attribute和反射的相關(guān)內(nèi)容,分享出來供大家參考學(xué)習(xí),下面話不多說了,來一起看看詳細的介紹吧。

Attribute(特性)

Attribute是C#的一種語言特性,用于為各種實體(class,field,property)附加一些說明性信息, 并且可以在運行時環(huán)境中檢索這些信息(通過反射)。

所有的Attribute必須繼承自Attribute類,按照約定,特性類的名稱帶有 Attribute 后綴。使用特性時可以包含或省略此后綴。

AttributeUsage

AttributeUsage是Attribute的Attribute,用于給自定義的Attribute加一些限定。

  • AttributeTargets
  • AllowMultiple
  • Inherited

AttributeTargets指定你這個attribute限制用于哪類實體上,在這里,實體是指: class、method、constructor、field、property、GenericParameter或者用All,表明可用于所有實體。每個target標記可以用|鏈接,如AttributeTargets.Class|AttributeTargets.Method表示這個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表明了這個attribute可否多次應(yīng)用于同一個實體,默認為false

[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)] 
class MultiUseAttr : Attribute { } 
 
[MultiUseAttr, MultiUseAttr] 
class Class2 { } 

Inherited

Inherited表明這個attribute是否可以被繼承傳遞,即子類或子類從父類繼承的成員是否帶這個attribute,默認為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ù)單元)就是一大堆的表,當編譯程序集或者模塊時,編譯器會創(chuàng)建一個類定義表,一個字段定義表,和一個方法定義表等。

反射是.Net中獲取運行時類型信息的方式,.Net的應(yīng)用程序由幾個部分:‘程序集(Assembly)'、‘模塊(Module)'、‘類型(class)'組成,而反射提供一種編程的方式,讓程序員可以在程序運行期獲得這幾個組成部分的相關(guān)信息, Assemblies contain modules. Modules contain classes. Classes contain functions.

System.reflection命名空間包含的幾個類,允許你反射(解析)這些元數(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


以下是上面幾個類的使用方法:

  • 使用Assembly定義和加載程序集,加載在程序集清單中列出模塊,以及從此程序集中查找類型并創(chuàng)建該類型的實例。
  • 使用Module了解包含模塊的程序集以及模塊中的類等,還可以獲取在模塊上定義的所有全局方法或其他特定的非全局方法。
  • 使用ConstructorInfo了解構(gòu)造函數(shù)的名稱、參數(shù)、訪問修飾符(如pulic 或private)和實現(xiàn)詳細信息(如abstract或virtual)等。
  • 使用Type的GetConstructors或 GetConstructor方法來調(diào)用特定的構(gòu)造函數(shù)。
  • 使用MethodInfo了解方法的名稱、返回類型、參數(shù)、訪問修飾符(如pulic 或private)和實現(xiàn)詳細信息(如abstract或virtual)等。
  • 使用Type的GetMethods或GetMethod方法來調(diào)用特定的方法。
  • 使用FiedInfo了解字段的名稱、訪問修飾符(如public或private)和實現(xiàn)詳細信息(如static)等,并獲取或設(shè)置字段值。
  • 使用EventInfo了解事件的名稱、事件處理程序數(shù)據(jù)類型、自定義屬性、聲明類型和反射類型等,添加或移除事件處理程序。
  • 使用PropertyInfo了解屬性的名稱、數(shù)據(jù)類型、聲明類型、反射類型和只讀或可寫狀態(tài)等,獲取或設(shè)置屬性值。
  • 使用ParameterInfo了解參數(shù)的名稱、數(shù)據(jù)類型、是輸入?yún)?shù)還是輸出參數(shù),以及參數(shù)在方法簽名中的位置等。

反射的作用:

  • 可以使用反射動態(tài)地創(chuàng)建類型的實例,將類型綁定到現(xiàn)有對象,或從現(xiàn)有對象中獲取類型
  • 應(yīng)用程序需要在運行時從某個特定的程序集中載入一個特定的類型,以便實現(xiàn)某個任務(wù)時可以用到反射。

使用反射獲取類型

public void Process(object processObj)
{
Type t = processsObj.GetType();
if(t.GetInterface(“ITest”) !=null)
     …
}

創(chuàng)建一個對象

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)建對象
object dObj = Activator.CreateInstance(t,constuctParms);
//獲取方法的信息
MethodInfo method = t.GetMethod("GetValue");
//調(diào)用方法的一些標志位,這里的含義是Public并且是實例方法,這也是默認的值
BindingFlags flag = BindingFlags.Public | BindingFlags.Instance;
//GetValue方法的參數(shù)
object[] parameters = new object[]{"Hello"};
//調(diào)用方法,用一個object接收返回值
object returnValue = method.Invoke(dObj,flag,Type.DefaultBinder,parameters,null);

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。

相關(guān)文章

  • C#通過oledb訪問access數(shù)據(jù)庫的方法

    C#通過oledb訪問access數(shù)據(jù)庫的方法

    這篇文章主要介紹了C#通過oledb訪問access數(shù)據(jù)庫的方法,實例分析了C#操作access數(shù)據(jù)庫的相關(guān)技巧,需要的朋友可以參考下
    2015-06-06
  • C#雙緩沖技術(shù)實例詳解

    C#雙緩沖技術(shù)實例詳解

    這篇文章主要介紹了C#雙緩沖技術(shù),結(jié)合實例形式較為詳細的分析了C#雙緩沖的功能,實現(xiàn)技巧與相關(guān)注意事項,需要的朋友可以參考下
    2016-02-02
  • C#處理類型和二進制數(shù)據(jù)轉(zhuǎn)換并提高程序性能

    C#處理類型和二進制數(shù)據(jù)轉(zhuǎn)換并提高程序性能

    這篇文章介紹了C#處理類型和二進制數(shù)據(jù)轉(zhuǎn)換并提高程序性能的方法,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-04-04
  • DevExpress設(shè)置TreeList圖片節(jié)點背景色的方法

    DevExpress設(shè)置TreeList圖片節(jié)點背景色的方法

    這篇文章主要介紹了DevExpress設(shè)置TreeList圖片節(jié)點背景色的方法,需要的朋友可以參考下
    2014-08-08
  • C#調(diào)用C++動態(tài)庫接口函數(shù)和回調(diào)函數(shù)方法

    C#調(diào)用C++動態(tài)庫接口函數(shù)和回調(diào)函數(shù)方法

    這篇文章主要介紹了C#調(diào)用C++動態(tài)庫接口函數(shù)和回調(diào)函數(shù)方法,通過C++端編寫接口展開內(nèi)容,文章介紹詳細具有一定的參考價值,需要的小伙伴可以參考一下
    2022-03-03
  • WPF實現(xiàn)類似ChatGPT逐字打印效果的示例代碼

    WPF實現(xiàn)類似ChatGPT逐字打印效果的示例代碼

    前一段時間ChatGPT類的應(yīng)用十分火爆,這類應(yīng)用在回答用戶的問題時逐字打印輸出,像極了真人打字回復(fù)消息,本文就來利用WPF模擬一下這種逐字打印的效果吧
    2023-08-08
  • C#讀寫txt文件的2種方法

    C#讀寫txt文件的2種方法

    這篇文章主要為大家詳細介紹了C#讀寫txt文本文檔數(shù)據(jù)的2種方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-10-10
  • c#采用toml做配置文件遇到的坑

    c#采用toml做配置文件遇到的坑

    這篇文章主要介紹了c#采用toml做配置文件遇到的坑,本文通過實例代碼給大家介紹的非常詳細,通過本文介紹得出c#用toml文件讀取非整數(shù)字請用double,不要用float,decimal倒無所謂,反正編譯不過,切記不要用float,需要的朋友可以參考下
    2024-04-04
  • C#中DataSet,DataTable,DataView的區(qū)別與用法

    C#中DataSet,DataTable,DataView的區(qū)別與用法

    這篇文章介紹了C#中DataSet,DataTable,DataView的區(qū)別與用法,文中通過示例代碼介紹的非常詳細。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-05-05
  • C# Winform實現(xiàn)截圖工具的示例代碼

    C# Winform實現(xiàn)截圖工具的示例代碼

    這篇文章主要為大家詳細介紹了如何使用C# Winform制作一個簡單的截圖工具,從而實現(xiàn)截圖功能,文中的示例代碼講解詳細,有需要的可以參考下
    2024-02-02

最新評論