MyClass類
namespace _014_反射和特性 { class MyClass { private int id; private int age; public int number; public string Name { get; set; } public string Name2 { get; set; } public string Name3 { get; set; }
public void Test1() {
} public void Test2() {
} } }
Main
using System; using System.Reflection;
namespace _014_反射和特性 { class Program { static void Main(string[] args) { //每一個類對應一個type對象,這個type對象存儲了這個類有哪些方法跟哪些數據哪些成員 //一個類中的數據是存儲在對象中的,但是type對象只存儲類的成員 MyClass my = new MyClass(); //通過對象獲取這個對象所屬類的Type對象 Type type = my.GetType(); //獲取類的名字 Console.WriteLine(type.Name); //獲取所在的命名空間 Console.WriteLine(type.Namespace); Console.WriteLine(type.Assembly); //只能獲取public 欄位 FieldInfo[] array = type.GetFields(); foreach (FieldInfo info in array) { Console.Write(info.Name + " "); } PropertyInfo[] array2 = type.GetProperties(); foreach (PropertyInfo info in array2) { Console.Write(info.Name + " "); } MethodInfo[] array3 = type.GetMethods(); foreach (MethodInfo info in array3) { Console.Write(info.Name + " "); } //通過type對象可以獲取它對應的類的所有成員(public) } } } 圖示 運行結果
namespace _014_反射和特性 { class Program { static void Main(string[] args) { MyClass my = new MyClass(); //通過類的type對象獲取它所在的程序集Assembly Assembly assem = my.GetType().Assembly; Console.WriteLine(assem.FullName); //輸出程序集中的類 Type[] types = assem.GetTypes(); foreach (var type in types) { Console.WriteLine(type); } Console.ReadKey(); } } } 圖示 運行效果
補充
//定義一個宏,才可以執行調用的方法 #define IsTest
using System; using System.Diagnostics;
namespace _015_特性 { class Program { //會編譯到程序集當中,但不會被調用 [Conditional("IsTest")] static void Test1() { Console.WriteLine("test1"); } static void Test2() { Console.WriteLine("test2"); } static void Main(string[] args) { Test1(); Test2(); Test1(); Test2(); Console.ReadKey(); } } } 圖示 運行結果
//定義一個宏,才可以執行調用的方法 //#define IsTest ...... 圖示 運行結果
//定義一個宏,才可以執行調用的方法 //#define IsTest ......
using System; using System.Runtime.CompilerServices;
namespace _015_特性 { class Program { static void PrintOut(string str, [CallerFilePath] string fileName = "", [CallerLineNumber] int lineNumber = 0, [CallerMemberName] string methodName = "") { Console.WriteLine(str); Console.WriteLine(fileName); Console.WriteLine(lineNumber); Console.WriteLine(methodName); } static void Main(string[] args) { PrintOut("123"); } } } 圖示 運行結果
我們在單步調試代碼的時候,常常希望調試器不要進入某些方法。我們只想執行該方法,然後繼續調試下一行。DebuggerStepThrough特性告訴調試器在執行目標代碼時不要進入該方法調試。有些方法小並且毫無疑問是正確的,在調試時對其反覆單步調試只能徒增煩惱。要小心使用該特性,不要排除了可能出現bug的代碼。
using System; using System.Diagnostics; using System.Runtime.CompilerServices;
namespace _015_特性 { class Program { //可以跳過debugger 的單步調試 不讓進入該方法 //(當我們確定這個方法沒有任何錯誤的時候,可以使用這個) [DebuggerStepThrough] static void PrintOut(string str,[CallerFilePath] string fileName="", [CallerLineNumber] int lineNumber=0,[CallerMemberName] string methodName ="") { Console.WriteLine(str); Console.WriteLine(fileName); Console.WriteLine(lineNumber); Console.WriteLine(methodName); } static void Main(string[] args) { PrintOut("123"); Console.ReadKey(); } } }
不添加DebuggerStepThrough特性
添加DebuggerStepThrough特性
MyTestAttribute類
using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Text; using System.Threading.Tasks;
namespace _015_特性 { //1, 特性類的後綴以Attribute結尾 //2, 需要繼承自System.Attribute //3, 一般情況下聲明為 sealed //4, 一般情況下 特性類用來表示目標結構的一些狀態(定義一些欄位或者屬性, 一般不定義方法) [AttributeUsage(AttributeTargets.Class)]//表示該特性類可以應用到的程序結構有哪些 sealed class MyTestAttribute : System.Attribute { public string Description { get; set; } public string VersionNumber { get; set; } public int ID { get; set; }
public MyTestAttribute(string des) { this.Description = des; } } }
Program類
using System; namespace _015_特性 { //通過制定屬性的名字,給屬性賦值,這種事命名參數 //當我們使用特性的時候,後面的Attribute不需要寫 [MyTest("簡單的特性類",ID = 100)] class Program { static void Main(string[] args) { //通過typeof+類名也可以獲取type對象 Type type = typeof (Program); object[] array = type.GetCustomAttributes(false); MyTestAttribute mytest = array[0] as MyTestAttribute; Console.WriteLine(mytest.Description); Console.WriteLine(mytest.ID); Console.ReadKey(); } } } 圖示 運行效果
using System; namespace _015_特性 { //通過制定屬性的名字,給屬性賦值,這種事命名參數 //當我們使用特性的時候,後面的Attribute不需要寫 [MyTest("簡單的特性類",ID = 100)] class Program { static void Main(string[] args) { //通過typeof+類名也可以獲取type對象 Type type = typeof (Program); object[] array = type.GetCustomAttributes(false); MyTestAttribute mytest = array[0] as MyTestAttribute; Console.WriteLine(mytest.Description); Console.WriteLine(mytest.ID); Console.ReadKey(); } } }