如何在C#中使用反射

本文转载自微信公众号「码农读书」,作者码农读书。转载本文请联系码农读书公众号。

创新互联建站是专业的尉氏网站建设公司,尉氏接单;提供网站设计、成都网站设计,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行尉氏网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!

C# 中的 反射 常用于在程序的运行时获取 类型 的元数据,可获取的信息包括已加载到进程中的 程序集 和 类型 信息,它和 C++ 中的 RTTI(Runtime Type Information) 的作用是差不多的。

为了能够使用反射,需要在项目中引用 System.Reflection 命名空间,在使用反射的开始,你会获取一个 Type 类型的对象,从这个对象上进一步获取 程序集,类型,模块 等信息,可以通过 反射 动态的生成某个类型的实例,甚至还能动态调用这个类型上的方法。

在 System.Reflection 命名空间下,定义了如下几大核心类型。

  • Assembly
  • Module
  • Enum
  • MethodInfo
  • ConstructorInfo
  • MemberInfo
  • ParameterInfo
  • Type
  • FieldInfo
  • EventInfo
  • PropertyInfo

现在我们一起研究一下怎么使用,考虑下面定义的 Customer 类。

 
 
 
 
  1. public class Customer
  2.     {
  3.         public int Id { get; set; }
  4.         public string FirstName { get; set; }
  5.         public string LastName { get; set; }
  6.         public string Address { get; set; }
  7.     }

下面的代码片段展示了如何通过 反射 来获取 Customer 的类名以及 Customer 的所属命名空间。

 
 
 
 
  1. class Program
  2.    {
  3.        static void Main(string[] args)
  4.        {
  5.            Type type = typeof(Customer);
  6.            Console.WriteLine("Class: " + type.Name);
  7.            Console.WriteLine("Namespace: " + type.Namespace);
  8.        }
  9.    }

再看一个例子,如何通过反射获取 Customer 下的所有属性,并且将属性名字全部展示在控制台上,如下代码所示:

 
 
 
 
  1. static void Main(string[] args)
  2.         {
  3.             Type type = typeof(Customer);
  4.             PropertyInfo[] propertyInfo = type.GetProperties();
  5.             Console.WriteLine("The list of properties of the Customer class are:--");
  6.             foreach (PropertyInfo pInfo in propertyInfo)
  7.             {
  8.                 Console.WriteLine(pInfo.Name);
  9.             }
  10.         }

值得注意的是,typeof(Customer).GetProperties() 默认只能获取 标记为 public 的属性集合,对应着 Customer 类下的四个公开属性。

接下来再来看看如何通过 反射 获取类型下的 构造函数 和 公共方法 的元数据信息,这里还是继续使用 Customer 类,在类中新增一个 构造函数 和一个 Validate 方法,此方法用于校验入参的合法性,下面就是修改后的 Customer 类。

 
 
 
 
  1. public class Customer
  2.     {
  3.         public int Id { get; set; }
  4.         public string FirstName { get; set; }
  5.         public string LastName { get; set; }
  6.         public string Address { get; set; }
  7.         public Customer() { }
  8.         public bool Validate(Customer customerObj)
  9.         {
  10.             //Code to validate the customer object
  11.             return true;
  12.         }
  13.     }

然后再来看看通过 反射 来获取 Customer 下所有定义的构造函数,不过这里只定义了一个构造函数,因此只能列出一个。

 
 
 
 
  1. class Program
  2.     {
  3.         static void Main(string[] args)
  4.         {
  5.             Type type = typeof(Customer);
  6.             ConstructorInfo[] constructorInfo = type.GetConstructors();
  7.             Console.WriteLine("The Customer class contains the following Constructors:--");
  8.             foreach (ConstructorInfo c in constructorInfo)
  9.             {
  10.                 Console.WriteLine(c);
  11.             }
  12.         }
  13.     }

同样也要注意,默认情况下 GetConstructors() 方法只能获取 Customer 的所有标记为 public 的构造函数。

接下来看看如何展示 Customer 中的所有 public 方法,因为该类中只定义了一个 public 方法,所以控制台上也应该只会展示一个,如下代码仅供参考。

 
 
 
 
  1. static void Main(string[] args)
  2.        {
  3.            Type type = typeof(Customer);
  4.            MethodInfo[] methodInfo = type.GetMethods();
  5.            Console.WriteLine("The methods of the Customer class are:--");
  6.            foreach (MethodInfo temp in methodInfo)
  7.            {
  8.                Console.WriteLine(temp.Name);
  9.            }
  10.            Console.Read();
  11.        }

是不是很惊讶,刚才还说是一个方法,居然多了好几个,要知道多的那几个方法,来自于两方面。

  • 从 object 类型继承下来的公共方法

  • 编译器自动生成的属性方法

如果方法上面标记了 Attribute, 还可以通过 GetCustomAttributes 方法来获取,参考代码如下:

 
 
 
 
  1. static void Main(string[] args)
  2.         {
  3.             foreach (MethodInfo temp in methodInfo)
  4.             {
  5.                 foreach (Attribute attribute in temp.GetCustomAttributes(true))
  6.                 {
  7.                     //Write your usual code here
  8.                 }
  9.             }
  10.         }

相信在你的应用程序中,经常会在 领域实体 上使用各种 Attribute 特性,这时候就可以通过上面的代码反射提取 领域实体 中的方法上的Attribute信息,从而根据提取到的 Attribute 执行你的具体业务逻辑。

译文链接:https://www.infoworld.com/article/3027240/how-to-work-with-reflection-in-c.html

分享名称:如何在C#中使用反射
浏览路径:http://www.shufengxianlan.com/qtweb/news7/97557.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联