• 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

  • 相关阅读:
    关于图片去水印和转载
    多叉树(森林)转二叉树
    [置顶] 读入优化&输出优化
    【NOI OJ】1816 拨钟问题
    Splay树
    【AVL】宠物收养所
    【树状数组】Stars
    对拍
    使用Mybatis进行多表联查操作
    搜索框自动提示
  • 原文地址:https://www.cnblogs.com/tlmbem/p/13676603.html
Copyright © 2020-2023  润新知