学习C#高级编程之反射和特性

绝地灬酷狼 2022-05-19 01:36 270阅读 0赞

1.程序时用来处理数据的,文本和特性都是数据,而我们程序本身(类的定义和BLC中的类)这也是数据。

2.有关程序及其类型的数据被称为元数据(metadata),它们保存在程序的程序集中。

3.程序在运行时,可以查看其它程序集或其本身的元数据。一个运行的程序查看本身的元数据或者其他程序集的元数据的行为叫做反射。

MyClass.cs

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace _019_反射和特性
  7. {
  8. class MyClass
  9. {
  10. private int id;
  11. private int age;
  12. public string Name1 { get; set; }
  13. public string Name2 { get; set; }
  14. public string Name3 { get; set; }
  15. public int number1;
  16. public int number2;
  17. public int number3;
  18. public void Test1()
  19. {
  20. }
  21. public void Test2()
  22. {
  23. }
  24. }
  25. }
  26. using System;
  27. using System.Collections.Generic;
  28. using System.Linq;
  29. using System.Text;
  30. using System.Reflection;
  31. using System.Threading.Tasks;
  32. namespace _019_反射和特性
  33. {
  34. class Program
  35. {
  36. static void Main(string[] args)
  37. {
  38. //每一个类对于一个type对象,这个type对象存储了这个类 有哪些方法跟哪些数据 哪些成员
  39. MyClass my = new MyClass();//一个类中的数据是存储在对象中的,但是type对象只存储类的成员
  40. Type type = my.GetType();//通过对象获取这个对象所属类的Type对象
  41. Console.WriteLine(type.Name);//获取类的名字
  42. Console.WriteLine(type.Namespace);//获取所在的命名空间
  43. Console.WriteLine(type.Assembly);//获取所在的程序集
  44. FieldInfo[] array = type.GetFields();//只能获取public字段
  45. foreach(FieldInfo info in array)
  46. {
  47. Console.Write(info.Name + " ");
  48. }
  49. PropertyInfo[] array2 = type.GetProperties();//获取属性
  50. foreach(PropertyInfo info in array2)
  51. {
  52. Console.Write(info.Name + " ");
  53. }
  54. MethodInfo[] array3 = type.GetMethods();//获取方法
  55. foreach(MethodInfo info in array3)
  56. {
  57. Console.Write(info.Name + " ");
  58. }
  59. //通过type对象可以获取它对应的类所有的成员(public)
  60. Console.ReadKey();
  61. }
  62. }
  63. }

特性

  1. #define IsTest//定义一个宏
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.Linq;
  6. using System.Runtime.CompilerServices;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace _020_特性
  10. {
  11. //通过指定熟悉的名字,给属性赋值,这种是命名参数
  12. [MyTest("简单的特性类",ID = 100)]//当我们使用特性的时候,后面的Attribute不需要写
  13. class Program
  14. {
  15. [Obsolete("这个方法过时了,使用NewMethod代替")]//Obsolete用来表示一个方法被弃用
  16. //[Obsolete("这个方法过时了,使用NewMethod代替",true)]//true表示不能使用,调用会显示错误
  17. static void OldMethod()
  18. {
  19. Console.WriteLine("Oldmethod");
  20. }
  21. static void NewMethod()
  22. {
  23. }
  24. [Conditional("IsTest")] //IsTest是一个宏,需要用#define定义,如果未定义调用就不会执行
  25. static void Test1()
  26. {
  27. Console.WriteLine("test1");
  28. }
  29. static void Test2()
  30. {
  31. Console.WriteLine("Test2");
  32. }
  33. //DebuggerStepthrough特性
  34. //调用者信息特性,可以获取文件名,调用方法和调用行数
  35. [DebuggerStepThrough]//可以跳过debugger的单步调试 不让进入该方法(当我们确定这个方法没有
  36. //任何错误的时候,可以使用这个)
  37. static void PrintOut(string str,[CallerFilePath]string fileName = "",[CallerLineNumber]
  38. int lineNumber = 0,[CallerMemberName]string methodName = "")
  39. {
  40. Console.WriteLine(str);
  41. Console.WriteLine(fileName);
  42. Console.WriteLine(lineNumber);
  43. Console.WriteLine(methodName);
  44. }
  45. static void Main(string[] args)
  46. {
  47. //OldMethod();
  48. //NewMethod();
  49. //Test1();
  50. //Test2();
  51. //Test1();
  52. //PrintOut("123");
  53. Type type = typeof(Program);//通过typeof+类名也可以获取type对象
  54. object[] array = type.GetCustomAttributes(false);//获取特性类对象数组
  55. MyTestAttribute mytest = array[0] as MyTestAttribute;
  56. Console.WriteLine(mytest.Description);
  57. Console.WriteLine(mytest.ID);
  58. Console.ReadKey();
  59. }
  60. }
  61. }

自定义特性类MyTestAttribute

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace _020_特性
  7. {
  8. //自定义特性类
  9. //1.特性类的后缀以Attribute结尾
  10. //2.需要继承自System.Attribute
  11. //3.一般情况下声明为sealed
  12. //4.一般情况下特性类用来表示目标结构的一些状态(定义一些字段或熟悉,一般不定义方法)
  13. [AttributeUsage(AttributeTargets.Class)]//表示该特性类可以应用到的程序结构有哪些
  14. sealed class MyTestAttribute:System.Attribute
  15. {
  16. public string Description { get; set; }
  17. public string VersionNumber { get; set; }
  18. public int ID { get; set; }
  19. public MyTestAttribute(string des)
  20. {
  21. this.Description = des;
  22. }
  23. }
  24. }

发表评论

表情:
评论列表 (有 0 条评论,270人围观)

还没有评论,来说两句吧...

相关阅读

    相关 JAVA高级特性——反射

    JAVA高级特性之——反射 一、什么是反射 JAVA反射机制是在运行状态中,对于任意一个雷,都能够知道这个类的所有属性和方法;对于任意一个对象,都能够调用它的任意方

    相关 学习C#高级编程进程线程

    1,计算机的核心是CPU,它承担了所有的计算任务。它就像一座工厂,时刻在运行。 2,如果工厂的电力有限一次只能供给一个车间使用。也就是说一个车间开工的时候,其他车间就必须停工