反射取值——特性、属性(C#)
C# 反射(Reflection)
反射指程序可以访问、检测和修改它本身状态或行为的一种能力。
程序集包含模块,而模块包含类型,类型又包含成员。反射则提供了封装程序集、模块和类型的对象。
您可以使用反射动态地创建类型的实例,将类型绑定到现有对象,或从现有对象中获取类型。然后,可以调用类型的方法或访问其字段和属性。
优缺点
优点:
1、反射提高了程序的灵活性和扩展性。
2、降低耦合性,提高自适应能力。
3、它允许程序创建和控制任何类的对象,无需提前硬编码目标类。
缺点:
1、性能问题:使用反射基本上是一种解释操作,用于字段和方法接入时要远慢于直接代码。因此反射机制主要应用在对灵活性和拓展性要求很高的系统框架上,普通程序不建议使用。
2、使用反射会模糊程序内部逻辑;程序员希望在源代码中看到程序的逻辑,反射却绕过了源代码的技术,因而会带来维护的问题,反射代码比相应的直接代码更复杂。
反射(Reflection)的用途
反射(Reflection)有下列用途:
它允许在运行时查看特性(attribute)信息。
它允许审查集合中的各种类型,以及实例化这些类型。
它允许延迟绑定的方法和属性(property)。
它允许在运行时创建新类型,然后使用这些类型执行一些任务。
接下来我们谈谈如何反射取值:
直接上代码
[Obsolete("123")]
class Program
{
// 定义一个奥特曼类
public class AutoMan
{
public int age { get; set; }
public string name { get; set; }
public short length { get; set; }
}
static void Main(string[] args)
{
// 实例化并给初值
AutoMan autoMan = new AutoMan()
{
age = 18,
name = "迪迦",
length = 3
};
Type type = autoMan.GetType();
PropertyInfo[] propertyInfos = type.GetProperties();
Console.WriteLine("奥特曼的属性值如下所示:");
foreach (PropertyInfo propertyInfo in propertyInfos)
{
object res = propertyInfo.GetValue(autoMan);
Console.Write(propertyInfo.Name + ": " + res.ToString() + " ");
}
Console.WriteLine();
Type type2 = typeof(Program);
IEnumerable<Attribute> attributes = type2.GetCustomAttributes(); // 获得Program类的所有特性
Console.WriteLine("Program的特性如下所示:");
foreach (Attribute attribute in attributes)
{
var d = attribute as ObsoleteAttribute; // Attribute是所有特性的父类,通过拆箱代替强转
if (d != null)
{
Console.Write(d.IsError + " ");
Console.Write(d.Message + " ");
Console.Write(d.TypeId + " ");
}
}
Console.WriteLine();
}
}
输出结果:
