Attribute特性 與 Reflection反射技術


今天就簡單的說下 Attribute(特性)和Reflection (反射),主要了解下以下基本就會用了。

Attribute 特性類
System.Reflection 反射名命名空間
Activator 類

1. Attribute(特性)

剛學的人都會覺得Attribute(特性)很神奇,因為加了某種特性就好像擁有了某種神奇的功能,但是查看特性類的定義又找不到讓它實現功能的邏輯代碼,你只看到幾句簡單的成員定義(構造函數、字段、屬性)就讓它擁有的某種特殊的功能。那是為什么呢,其實說白了特性只是說明描述,他只是被保存到元數據的描述而已。但他的描述只能是能在編譯的時候被編譯時能確定的常量類型的描述。那它的功能在哪里實現呢,等下講到Reflection (反射)會講到。因為特性本身只包含描述值但不包含功能。

 

2. Reflection (反射)

而剛接觸 Reflection (反射)卻覺得沒有什么用,不知道在哪里用。其實反射到處都在使用。只是我們沒有細心發現而已。如果你看過WinForm控件或WebForm控件的源碼你就會發現微軟為了能讓我們這些開發者能提高開發效率,讓 Visual Studio 開發工具 把 Attribute(特性)和Reflection (反射)發揮的淋漓盡致。這就是為什么有智能提示功能,可視化設計編輯器功能 的存在。反射用法有以下

1 提取對象的行為成員(方法),對他們進行調用;提取對象的數據成員(字段、屬性),對他們進行修改。

2  在某一些方法里提取對象或對象成員的Attribute(特性)描述進行一些邏輯上的處理。

但需要注意的是 Reflection (反射)是會破壞對象編程語言的封裝概念。例如我們平常在Visual Studio 編輯器寫代碼對象訪問權都有 private  protected  internal  public 這些訪問修飾符,編譯器都會幫我們檢查語法和訪問權的問題。但是利用反射技術就可以繞過修飾符直接訪問。例如在對象的成員中(字段) private readonly 修飾符 修飾的字段一般都不希望別人修改,但利用反射技術就能暮改。可以說除了常量以外都能改。

 

一般來說 Attribute(特性) 和 Reflection (反射)都是配合一起來使用的。需要注意的Attribute(特性)的定義不會帶來性能的損耗,但 Reflection (反射)一般會降低程序性能的。但有些情況也有利用緩存來緩解這種弊端,一般只會在第一次調用特性時耗時。

 

下面我們就舉下反射常用的方式

    1.Assembly
    2.類型
    3.構造函數
    4.字段
    5.屬性
    6.方法
    7.枚舉
    8.事件
    9.成員

Assembly程序集信息

 1     #region 獲取Assembly方式
 2  Console.WriteLine();
 3             Console.WriteLine("==============獲取Assembly方式==================");
 4 
 5             //獲取當前代碼所在程序集
 6             Assembly assembly1 = Assembly.GetExecutingAssembly();
 7  Console.WriteLine(assembly1.FullName);
 8 
 9             //獲取應用程序域所有程序集
10             Assembly[] assembly2 = AppDomain.CurrentDomain.GetAssemblies();
11             foreach (Assembly item in assembly2)
12             {
13  Console.WriteLine(item.FullName);
14             }
15 
16             //通過長格式名稱(包括程序集名,版本信息,語言文化,公鑰標記)加載程序集(不會造成重復加載的問題)
17             Assembly assembly3 = Assembly.Load("ReflectionLibrary, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null");
18  Console.WriteLine(assembly3.FullName);
19 
20             //已知程序集的文件名或路徑,加載程序集(不會造成重復加載的問題)(會加載此程序集引用的其他程序集)
21             Assembly assembly4 = Assembly.LoadFrom(AppDomain.CurrentDomain.BaseDirectory + "ReflectionLibrary.dll");
22  Console.WriteLine(assembly4.FullName);
23 
24             //加載指定路徑上的程序集文件的內容(會造成重復加載的問題)(不會加載此程序集引用的其他程序集)
25             Assembly assembly5 = Assembly.LoadFile(AppDomain.CurrentDomain.BaseDirectory + "ReflectionLibrary.dll");
26  Console.WriteLine(assembly5.FullName);
27 
28             //從內嵌資源加載程序集
29             Stream libraryStream = assembly1.GetManifestResourceStream(assembly1.GetManifestResourceNames().FirstOrDefault(a => a.Contains("ReflectionLibrary")));
30             byte[] libraryByte = new byte[libraryStream.Length];
31             libraryStream.Read(libraryByte, 0, libraryByte.Length);
32             Assembly assembly6 = Assembly.Load(libraryByte);
33  Console.WriteLine(assembly6.FullName);
34 
35             #endregion

類型

 1       #region 獲取類型方式
 2  Console.WriteLine();
 3             Console.WriteLine("==============獲取類型方式==================");
 4 
 5             //根據類型定義獲取
 6             Type type1 = typeof(ReflectionClass);
 7  Console.WriteLine(type1.FullName);
 8 
 9             //根據類型長名稱(命名空間.名稱)獲取
10             Type type2 = Type.GetType("ReflectionConsole.ReflectionClass");
11  Console.WriteLine(type2.FullName);
12 
13             //根據實例獲取
14             ReflectionClass type_obj = new ReflectionClass();
15             Type type3 = type_obj.GetType();
16  Console.WriteLine(type3.FullName);
17 
18             #endregion

構造函數

 1       #region 構造函數
 2  Console.WriteLine();
 3             Console.WriteLine("==============構造函數==================");
 4 
 5             //獲取公共實例構造函數。
 6             ConstructorInfo[] public_constructor_arr = type1.GetConstructors();
 7             foreach (ConstructorInfo item in public_constructor_arr)
 8             {
 9                 Console.WriteLine("public構造函數名稱: " + item.Name);
10             }
11 
12             //獲取指定簽名的構造函數
13             ConstructorInfo private_constructor = type1.GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, new Type[] { typeof(string), typeof(string) }, null);//private ReflectionClass(object value1, object value2)構造函數
14             Console.WriteLine("private構造函數名稱: " + private_constructor.Name);
15 
16             //調用構造函數創建對象方式1
17             ReflectionClass private_constructor_obj = (ReflectionClass)private_constructor.Invoke(new object[] { 1, 2 });
18             //調用構造函數創建對象方式2(必須public)
19             ReflectionClass public_constructor_obj = (ReflectionClass)Activator.CreateInstance(type1, new object[] { 1 });
20 
21             #endregion

字段

 1     #region 字段
 2  Console.WriteLine();
 3             Console.WriteLine("==============字段==================");
 4 
 5             //獲取所有公共字段。
 6             FieldInfo[] public_field_arr = type1.GetFields();
 7             foreach (FieldInfo item in public_field_arr)
 8             {
 9                 Console.WriteLine("public字段名稱: " + item.Name);
10             }
11 
12             //獲取指定字段
13             FieldInfo private_field = type1.GetField("field_private", BindingFlags.NonPublic | BindingFlags.Instance);
14             Console.WriteLine("private字段名稱: " + private_field.Name);
15 
16             //獲取指定字段的值
17             string private_field_value = (string)private_field.GetValue(obj);
18             Console.WriteLine("字段修改前值:" + private_field_value);
19             //設置指定字段的值
20             private_field.SetValue(obj, "【field_private_newvalue】");
21             Console.WriteLine("字段修改后值:" + (string)private_field.GetValue(obj));
22 
23             //獲取指定靜態字段值
24             FieldInfo public_static_field_value = type1.GetField("field_public_static", BindingFlags.Public | BindingFlags.Static);
25             Console.WriteLine("靜態字段值: " + public_static_field_value.GetValue(null));
26 
27 
28             //獲取指定字段上的所有特性
29             object[] private_field_attribute_arr1 = private_field.GetCustomAttributes(false);
30             foreach (Attribute item in private_field_attribute_arr1)
31             {
32                 Console.WriteLine("特性名稱" + item.GetType().Name);
33             }
34 
35             //獲取指定字段上的指定類型特性和特性參數
36             field_attribute_1[] private_field_attribute_arr2 = (field_attribute_1[])private_field.GetCustomAttributes(typeof(field_attribute_1), false);
37             foreach (field_attribute_1 item in private_field_attribute_arr2)
38             {
39                 Console.WriteLine(String.Format("特性名稱:{0}  特性參數值: {1}", item.GetType().Name, item.name));
40             }
41 
42             #endregion

屬性

 1   #region 屬性
 2  Console.WriteLine();
 3             Console.WriteLine("==============屬性==================");
 4 
 5             //獲取所有公共屬性。
 6             PropertyInfo[] public_property_arr = type1.GetProperties();
 7             foreach (PropertyInfo item in public_property_arr)
 8             {
 9                 Console.WriteLine("public屬性名稱: " + item.Name);
10             }
11 
12             //獲取指定屬性
13             PropertyInfo private_property = type1.GetProperty("property_private", BindingFlags.NonPublic | BindingFlags.Instance);
14             Console.WriteLine("private屬性名稱: " + private_property.Name);
15 
16             //獲取指定屬性的值
17             string private_property_value = (string)private_property.GetValue(obj, null);
18             Console.WriteLine("屬性修改前值:" + private_property_value);
19             //設置指定屬性的值
20             private_property.SetValue(obj, "【property_private_newvalue】", null);
21             Console.WriteLine("屬性修改后值:" + (string)private_property.GetValue(obj, null));
22 
23             //獲取指定靜態屬性值
24             PropertyInfo public_static_property_value = type1.GetProperty("property_public_static", BindingFlags.Public | BindingFlags.Static);
25             Console.WriteLine("靜態屬性值: " + public_static_property_value.GetValue(null, null));
26 
27 
28             //獲取指定屬性上的所有特性
29             object[] private_property_attribute_arr1 = private_property.GetCustomAttributes(false);
30             foreach (Attribute item in private_property_attribute_arr1)
31             {
32                 Console.WriteLine("特性名稱" + item.GetType().Name);
33             }
34 
35             //獲取指定屬性上的指定類型特性和特性參數
36             property_attribute_1[] private_property_attribute_arr2 = (property_attribute_1[])private_property.GetCustomAttributes(typeof(property_attribute_1), false);
37             foreach (property_attribute_1 item in private_property_attribute_arr2)
38             {
39                 Console.WriteLine(String.Format("特性名稱:{0}  特性參數值: {1}", item.GetType().Name, item.name));
40             }
41 
42             #endregion

方法

 1      #region 方法
 2  Console.WriteLine();
 3             Console.WriteLine("==============方法==================");
 4 
 5             //獲取所有公共方法
 6             MethodInfo[] public_method_arr = type1.GetMethods();
 7             foreach (MethodInfo item in public_method_arr)
 8             {
 9                 Console.WriteLine("方法名稱: " + item.Name);
10             }
11 
12             //獲取指定方法
13             MethodInfo private_method = type1.GetMethod("function_private", BindingFlags.NonPublic | BindingFlags.Instance);//獲取private object function_private(object value)
14 
15             //執行指定方法的方式1
16             object function_private_result1 = private_method.Invoke(obj, new object[] { "function_private" });
17             Console.WriteLine("執行方法的方式1: " + function_private_result1);
18 
19             //執行指定方法的方式2
20             object function_private_result2 = type1.InvokeMember("function_private", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.InvokeMethod, null, obj, new object[] { "function_private" });
21             Console.WriteLine("執行方法的方式2: " + function_private_result1);
22 
23             //獲取指定方法上指定特性
24             object[] private_method_attribute_arr = private_method.GetCustomAttributes(typeof(method_attribute), false);
25             foreach (method_attribute item in private_method_attribute_arr)
26             {
27                 Console.WriteLine("方法特性參數值: " + item.name);
28             }
29 
30             #endregion

枚舉

 1      #region  枚舉
 2  Console.WriteLine();
 3             Console.WriteLine("==============枚舉==================");
 4 
 5             Type enum_type = typeof(WeekEnum);
 6 
 7             //獲取所有枚舉成員的名稱
 8             string[] enum_name_arr = enum_type.GetEnumNames();
 9             foreach (string item in enum_name_arr)
10             {
11                 Console.WriteLine("枚舉成員名稱: " + item);
12             }
13 
14             //獲取所有枚舉成員的值
15             Array enum_value_arr = enum_type.GetEnumValues();
16             foreach (object enum_item in enum_value_arr)
17             {
18                 Console.WriteLine("枚舉成員值: " + (int)Enum.Parse(typeof(WeekEnum), enum_item.ToString()));
19             }
20 
21             //獲取枚舉成員的指定特性
22             FieldInfo[] enum_field_arr = enum_type.GetFields();//這里用GetFields因為枚舉成員是常量字段
23             foreach (FieldInfo item in enum_field_arr)
24             {
25                 object[] enum_field_attribute_arr = item.GetCustomAttributes(typeof(enum_attribute), false);
26                 string enum_str = "";
27                 foreach (enum_attribute attribute_item in enum_field_attribute_arr)
28                 {
29                     enum_str += "枚舉值特性參數值: " + attribute_item.name + " ";
30                 }
31                 Console.WriteLine("枚舉成員名稱: " + item.Name + " " + enum_str);
32             }
33 
34             #endregion

 事件

 1        #region 事件          
 2  Console.WriteLine();
 3             Console.WriteLine("==============事件==================");
 4             MethodInfo execute_event_test = type1.GetMethod("execute_event_function_public", BindingFlags.Public | BindingFlags.Instance);//事件測試觸發
 5 
 6             //獲取所有公共事件
 7             EventInfo[] public_event_arr = type1.GetEvents();
 8             foreach (EventInfo item in public_event_arr)
 9             {
10                 Console.WriteLine("public事件名稱: " + item.Name);
11             }
12 
13             //綁定一個公共方法(可以重復綁定相同方法)
14             EventInfo public_event = type1.GetEvent("custom_delegate_public", BindingFlags.Public | BindingFlags.Instance);
15             MethodInfo public_event_method_add = type1.GetMethod("event_function_public", BindingFlags.Public | BindingFlags.Instance);
16             public_event.AddEventHandler(obj, Delegate.CreateDelegate(public_event.EventHandlerType, obj, public_event_method_add));
17             execute_event_test.Invoke(obj, new object[] { "custom_delegate_public" });
18 
19             //綁定一個非公共方法(可以重復綁定相同方法)
20             MethodInfo private_event_method_add = type1.GetMethod("event_function_private", BindingFlags.NonPublic | BindingFlags.Instance);
21             public_event.GetAddMethod(true);//true是才能綁定非公開方法
22             public_event.AddEventHandler(obj, Delegate.CreateDelegate(public_event.EventHandlerType, obj, private_event_method_add));
23             execute_event_test.Invoke(obj, new object[] { "custom_delegate_public" });
24 
25             //移除一個公共方法(相同重復方法只會移除一個)
26             MethodInfo public_event_method_remove = type1.GetMethod("event_function_public", BindingFlags.Public | BindingFlags.Instance);
27             public_event.RemoveEventHandler(obj, Delegate.CreateDelegate(public_event.EventHandlerType, obj, public_event_method_remove));
28             execute_event_test.Invoke(obj, new object[] { "custom_delegate_public" });
29 
30             //移除一個非公共方法(相同重復方法只會移除一個)
31             MethodInfo private_event_method_remove = type1.GetMethod("event_function_private", BindingFlags.NonPublic | BindingFlags.Instance);
32             public_event.GetRemoveMethod(true);//true是才能移除非公開方法
33             public_event.RemoveEventHandler(obj, Delegate.CreateDelegate(public_event.EventHandlerType, obj, private_event_method_remove));
34             execute_event_test.Invoke(obj, new object[] { "custom_delegate_public" });
35 
36             //移除所有的方法
37             FieldInfo event_private_field = type1.GetField("_custom_delegate_public", BindingFlags.Instance | BindingFlags.NonPublic);//用GetField獲取事件的私有字段
38             object event_field_value = event_private_field.GetValue(obj);
39             if (event_field_value != null)
40             {
41                 Delegate[] delegate_arr = ((ReflectionClass.custom_delegate)event_field_value).GetInvocationList();//獲取事件私有字段所有已注冊的方法
42                 foreach (Delegate item in delegate_arr)
43                 {
44                     public_event.RemoveEventHandler(obj, item);
45                 }
46             }
47             execute_event_test.Invoke(obj, new object[] { "custom_delegate_public" });
48 
49             //獲取事件的指定特性
50             object[] event_attribute_arr = public_event.GetCustomAttributes(typeof(event_attribute), false);
51             foreach (event_attribute item in event_attribute_arr)
52             {
53                 Console.WriteLine("事件特性參數值: " + item.name);
54             }
55 
56             #endregion

成員

 1    #region 成員
 2  Console.WriteLine();
 3             Console.WriteLine("==============成員==================");
 4 
 5             //獲取所有公共成員
 6             MemberInfo[] public_member_arr1 = type1.GetMembers();
 7             foreach (MemberInfo item in public_member_arr1)
 8             {
 9                 Console.WriteLine("成員名稱: " + item.Name + "   成員類別: " + item.MemberType.ToString());
10             }
11 
12             //獲取具有指定特性的成員
13             MemberInfo[] public_member_arr2 = type1.GetMembers(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
14             foreach (MemberInfo item in public_member_arr2)
15             {
16                 if (item.IsDefined(typeof(field_attribute_2), false))
17                 {
18                     Console.WriteLine("獲取具有指定特性值的成員: " + item.Name);
19                 }
20             }
21 
22             //獲取具有指定特性值的成員
23             MemberInfo[] public_member_arr3 = type1.GetMembers(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
24             foreach (MemberInfo item in public_member_arr3)
25             {
26                 field_attribute_1[] private_property_attribute_arr = (field_attribute_1[])item.GetCustomAttributes(typeof(field_attribute_1), false);
27 
28                 if (private_property_attribute_arr != null)
29                 {
30                     foreach (field_attribute_1 attribute_item in private_property_attribute_arr)
31                     {
32                         if (attribute_item.name == "private字段特性值1")
33                         {
34                             Console.WriteLine("獲取具有指定特性值的成員: " + item.Name);
35                         }
36 
37                     }
38                 }
39 
40             }
41 
42             #endregion

 

  1 namespace ReflectionConsole
  2 {
  3     public class ReflectionClass : ReflectionBaseClass
  4     {
  5         #region 事件
  6 
  7         public delegate void custom_delegate(object value);
  8 
  9         private event custom_delegate _custom_delegate_private;
 10         [event_attribute("自定義private事件特性參數")]
 11         private event custom_delegate custom_delegate_private
 12         {
 13             add { this._custom_delegate_private += value; }
 14             remove { this._custom_delegate_private -= value; }
 15         }
 16 
 17         private event custom_delegate _custom_delegate_public;
 18         [event_attribute("自定義public事件特性參數")]
 19         public event custom_delegate custom_delegate_public
 20         {
 21             add { this._custom_delegate_public += value; }
 22             remove { this._custom_delegate_public -= value; }
 23         }
 24 
 25         private void event_function_private(object value)
 26         {
 27             Console.WriteLine("執行event_function_private方法");
 28         }
 29 
 30         public void event_function_public(object value)
 31         {
 32             Console.WriteLine("執行event_function_public方法 ");
 33         }
 34 
 35         public void execute_event_function_public(object value)
 36         {
 37             Console.WriteLine();
 38             Console.WriteLine("=======" + value + "事件======開始");
 39             if (this._custom_delegate_public != null)
 40             {
 41                 this._custom_delegate_public(value);
 42             }
 43             Console.WriteLine("=======" + value + "事件======結束");
 44         }
 45 
 46         #endregion
 47 
 48         #region 字段
 49 
 50         [field_attribute_1("private字段特性值1")]
 51         [field_attribute_2("private字段特性值2")]
 52         private string field_private = "【field_private】";
 53         public string field_public = "【field_public】";
 54         public static string field_public_static = "【field_public_static】";
 55 
 56         protected string field_protected = "【field_protected】";
 57         internal string field_internal = "【field_internal】";
 58         protected internal string field_protected_internal = "【field_protected_internal】";
 59 
 60         #endregion
 61 
 62         #region 屬性
 63 
 64         private string _property_private = "【_property_private】";
 65         [property_attribute_1("private屬性1")]
 66         [property_attribute_2("private屬性2")]
 67         private string property_private
 68         {
 69             get { return this._property_private; }
 70             set { this._property_private = value; }
 71         }
 72         private string _property_public = "【_property_public】";
 73         [property_attribute_1("public屬性1")]
 74         [property_attribute_2("private屬性2")]
 75         public string property_public
 76         {
 77             get { return this._property_public; }
 78             set { this._property_public = value; }
 79         }
 80         private static string _property_public_static = "【_property_public_static】";
 81         [property_attribute_1("public_static屬性1")]
 82         [property_attribute_2("private屬性2")]
 83         public static string property_public_static
 84         {
 85             get { return _property_public_static; }
 86             set { _property_public_static = value; }
 87         }
 88 
 89 
 90 
 91         private string _property_protected = "【_property_protected】";
 92         protected string property_protected
 93         {
 94             get { return this._property_protected; }
 95             set { this._property_protected = value; }
 96         }
 97         private string _property_internal = "【_property_internal】";
 98         internal string property_internal
 99         {
100             get { return this._property_internal; }
101             set { this._property_internal = value; }
102         }
103         private string _property_protected_internal = "【_property_protected_internal】";
104         protected internal string property_protected_internal
105         {
106             get { return this._property_protected_internal; }
107             set { this._property_protected_internal = value; }
108         }
109 
110         #endregion
111 
112         #region 構造函數
113 
114         public ReflectionClass()
115         {
116 
117         }
118 
119         public ReflectionClass(object value)
120         {
121 
122         }
123 
124         private ReflectionClass(object value1, object value2)
125         {
126 
127         }
128 
129         static ReflectionClass()
130         {
131 
132         }
133 
134         #endregion
135 
136         #region 方法
137 
138         [method_attribute("private方法特性參數值1")]
139         [method_attribute("private方法特性參數值2")]
140         private object function_private(object value)
141         {
142             return value;
143         }
144         public object function_public(object value)
145         {
146             return value;
147         }
148         public static object function_public_static(object value)
149         {
150             return value;
151         }
152 
153         protected void function_protected()
154         {
155 
156         }
157         internal object function_internal(object value)
158         {
159             return value;
160         }
161         protected internal object function_protected_internal(object value1, object value2)
162         {
163             return value1 + "  " + value2;
164         }
165 
166         #endregion
167     }
168 
169     /// <summary>
170     /// 自定義反射基類
171     /// </summary>
172     public class ReflectionBaseClass
173     {
174         #region 字段
175         [field_attribute_1("private字段特性值1")]
176         [field_attribute_2("private字段特性值2")]
177         private string base_field_private = "【base_field_private】";
178         public string base_field_public = "【base_field_public】";
179         public static string field_public_static = "【field_public_static】";
180 
181         protected string base_field_protected = "【base_field_protected】";
182         internal string base_field_internal = "【base_field_internal】";
183         protected internal string base_field_protected_internal = "【base_field_protected_internal】";
184 
185         #endregion
186 
187         #region 屬性
188 
189         private string _base_property_private = "【_base_property_private】";
190         [property_attribute_1("private屬性1")]
191         [property_attribute_2("private屬性2")]
192         private string base_property_private
193         {
194             get { return this._base_property_private; }
195             set { this._base_property_private = value; }
196         }
197         private string _base_property_public = "【_base_property_public】";
198         [property_attribute_1("private屬性1")]
199         [property_attribute_2("private屬性2")]
200         public string base_property_public
201         {
202             get { return this._base_property_public; }
203             set { this._base_property_public = value; }
204         }
205         private static string _base_property_public_static = "【_base_property_public_static】";
206         [property_attribute_1("private屬性1")]
207         [property_attribute_2("private屬性2")]
208         public static string base_property_public_static
209         {
210             get { return _base_property_public_static; }
211             set { _base_property_public_static = value; }
212         }
213 
214 
215 
216         private string _base_property_protected = "【_base_property_protected】";
217         protected string base_property_protected
218         {
219             get { return this._base_property_protected; }
220             set { this._base_property_protected = value; }
221         }
222         private string _base_property_internal = "【_base_property_internal】";
223         internal string base_property_internal
224         {
225             get { return this._base_property_internal; }
226             set { this._base_property_internal = value; }
227         }
228         private string _base_property_protected_internal = "【_base_property_protected_internal】";
229         protected internal string base_property_protected_internal
230         {
231             get { return this._base_property_protected_internal; }
232             set { this._base_property_protected_internal = value; }
233         }
234 
235         #endregion
236 
237         #region 方法
238 
239         private object base_function_private(object value)
240         {
241             return value;
242         }
243         public object base_function_public(object value)
244         {
245             return value;
246         }
247         public static object base_function_public_static(object value)
248         {
249             return value;
250         }
251 
252         protected void base_function_protected()
253         {
254 
255         }
256         internal object base_function_internal(object value)
257         {
258             return value;
259         }
260         protected internal object base_function_protected_internal(object value1, object value2)
261         {
262             return value1 + "  " + value2;
263         }
264 
265         #endregion
266     }
267 
268     /// <summary>
269     /// 星期枚舉
270     /// </summary>
271     public enum WeekEnum : int
272     {
273         [enum_attribute("星期一")]
274         Monday = 0,
275         [enum_attribute("星期二")]
276         Tuesday = 1,
277         [enum_attribute("星期三")]
278         Wednesday = 2,
279         [enum_attribute("星期四")]
280         Thursday = 3,
281         [enum_attribute("星期五")]
282         Friday = 4,
283         [enum_attribute("星期六")]
284         Saturday = 5,
285         [enum_attribute("星期日")]
286         Sunday = 6
287     }
288 
289     /// <summary>
290     /// 字段特性1
291     /// </summary>
292     [AttributeUsageAttribute(AttributeTargets.Field, Inherited = true, AllowMultiple = true)]
293     public class field_attribute_1 : Attribute
294     {
295         public string name;
296 
297         public field_attribute_1(string name)
298         {
299             this.name = name;
300         }
301 
302     }
303 
304     /// <summary>
305     /// 字段特性2
306     /// </summary>
307     [AttributeUsageAttribute(AttributeTargets.Field, Inherited = true, AllowMultiple = true)]
308     public class field_attribute_2 : Attribute
309     {
310         public string name;
311 
312         public field_attribute_2(string name)
313         {
314             this.name = name;
315         }
316 
317     }
318     /// <summary>
319     /// 屬性特性1
320     /// </summary>
321     [AttributeUsageAttribute(AttributeTargets.Property, Inherited = true, AllowMultiple = true)]
322     public class property_attribute_1 : Attribute
323     {
324         public string name;
325 
326         public property_attribute_1(string name)
327         {
328             this.name = name;
329         }
330 
331     }
332     /// <summary>
333     /// 屬性特性2
334     /// </summary>
335     [AttributeUsageAttribute(AttributeTargets.Property, Inherited = true, AllowMultiple = true)]
336     public class property_attribute_2 : Attribute
337     {
338         public string name;
339 
340         public property_attribute_2(string name)
341         {
342             this.name = name;
343         }
344 
345     }
346 
347     /// <summary>
348     /// 方法特性
349     /// </summary>
350     [AttributeUsageAttribute(AttributeTargets.Method, Inherited = true, AllowMultiple = true)]
351     public class method_attribute : Attribute
352     {
353         public string name;
354 
355         public method_attribute(string name)
356         {
357             this.name = name;
358         }
359 
360     }
361 
362     /// <summary>
363     /// 事件特性
364     /// </summary>
365     [AttributeUsageAttribute(AttributeTargets.Event, Inherited = true, AllowMultiple = true)]
366     public class event_attribute : Attribute
367     {
368         public string name;
369 
370         public event_attribute(string name)
371         {
372             this.name = name;
373         }
374 
375     }
376 
377     /// <summary>
378     /// 枚舉值特性
379     /// </summary>
380     [AttributeUsageAttribute(AttributeTargets.Field, Inherited = true, AllowMultiple = true)]
381     public class enum_attribute : Attribute
382     {
383         public string name;
384 
385         public enum_attribute(string name)
386         {
387             this.name = name;
388         }
389 
390     }
391 
392 }
基類代碼

 

源碼下載:特性與反射.zip


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM