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

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

System.Reflection 命名空间下,定义了如下几大外围类型。

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

当初咱们一起钻研一下怎么应用,思考上面定义的 Customer 类。

    public class Customer    {        public int Id { get; set; }        public string FirstName { get; set; }        public string LastName { get; set; }        public string Address { get; set; }    }

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

    class Program    {        static void Main(string[] args)        {            Type type = typeof(Customer);            Console.WriteLine("Class: " + type.Name);            Console.WriteLine("Namespace: " + type.Namespace);        }    }

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

        static void Main(string[] args)        {            Type type = typeof(Customer);            PropertyInfo[] propertyInfo = type.GetProperties();            Console.WriteLine("The list of properties of the Customer class are:--");            foreach (PropertyInfo pInfo in propertyInfo)            {                Console.WriteLine(pInfo.Name);            }        }

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

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

    public class Customer    {        public int Id { get; set; }        public string FirstName { get; set; }        public string LastName { get; set; }        public string Address { get; set; }        public Customer() { }        public bool Validate(Customer customerObj)        {            //Code to validate the customer object            return true;        }    }

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

    class Program    {        static void Main(string[] args)        {            Type type = typeof(Customer);            ConstructorInfo[] constructorInfo = type.GetConstructors();            Console.WriteLine("The Customer class contains the following Constructors:--");            foreach (ConstructorInfo c in constructorInfo)            {                Console.WriteLine(c);            }        }    }

同样也要留神,默认状况下 GetConstructors() 办法只能获取 Customer 的所有标记为 public 的构造函数。

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

        static void Main(string[] args)        {            Type type = typeof(Customer);            MethodInfo[] methodInfo = type.GetMethods();            Console.WriteLine("The methods of the Customer class are:--");            foreach (MethodInfo temp in methodInfo)            {                Console.WriteLine(temp.Name);            }            Console.Read();        }

是不是很诧异,方才还说是一个办法,竟然多了好几个,要晓得多的那几个办法,来自于两方面。

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

  • 编译器主动生成的属性办法

如果办法下面标记了 Attribute, 还能够通过 GetCustomAttributes 办法来获取,参考代码如下:

        static void Main(string[] args)        {            foreach (MethodInfo temp in methodInfo)            {                foreach (Attribute attribute in temp.GetCustomAttributes(true))                {                    //Write your usual code here                }            }        }

置信在你的应用程序中,常常会在 畛域实体 上应用各种 Attribute 个性,这时候就能够通过下面的代码反射提取 畛域实体 中的办法上的Attribute信息,从而依据提取到的 Attribute 执行你的具体业务逻辑。

译文链接:https://www.infoworld.com/art...

更多高质量干货:参见我的 GitHub: csharptranslate