菜鸟教程 -- 学的不仅是技术,更是梦想!

C# 教程
(追記) (追記ここまで)

C# 反射(Reflection)

反射指程序可以访问、检测和修改它本身状态或行为的一种能力。

程序集包含模块,而模块包含类型,类型又包含成员。反射则提供了封装程序集、模块和类型的对象。

您可以使用反射动态地创建类型的实例,将类型绑定到现有对象,或从现有对象中获取类型。然后,可以调用类型的方法或访问其字段和属性。

优缺点

优点:

  • 1、反射提高了程序的灵活性和扩展性。
  • 2、降低耦合性,提高自适应能力。
  • 3、它允许程序创建和控制任何类的对象,无需提前硬编码目标类。

缺点:

  • 1、性能问题:使用反射基本上是一种解释操作,用于字段和方法接入时要远慢于直接代码。因此反射机制主要应用在对灵活性和拓展性要求很高的系统框架上,普通程序不建议使用。
  • 2、使用反射会模糊程序内部逻辑;程序员希望在源代码中看到程序的逻辑,反射却绕过了源代码的技术,因而会带来维护的问题,反射代码比相应的直接代码更复杂。

反射(Reflection)的用途

反射(Reflection)有下列用途:

  • 它允许在运行时查看特性(attribute)信息。
  • 它允许审查集合中的各种类型,以及实例化这些类型。
  • 它允许延迟绑定的方法和属性(property)。
  • 它允许在运行时创建新类型,然后使用这些类型执行一些任务。

查看元数据

我们已经在上面的章节中提到过,使用反射(Reflection)可以查看特性(attribute)信息。

System.Reflection 类的 MemberInfo 对象需要被初始化,用于发现与类相关的特性(attribute)。为了做到这点,您可以定义目标类的一个对象,如下:

System.Reflection.MemberInfo info = typeof(MyClass);

下面的程序演示了这点:

实例

using System;

[AttributeUsage(AttributeTargets.All)]
public class HelpAttribute : System.Attribute
{
public readonly string Url;

public string Topic // Topic 是一个命名(named)参数
{
get
{
return topic;
}
set
{

topic = value;
}
}

public HelpAttribute(string url) // url 是一个定位(positional)参数
{
this.Url = url;
}

private string topic;
}
[HelpAttribute("Information on the class MyClass")]
class MyClass
{
}

namespace AttributeAppl
{
class Program
{
static void Main(string[] args)
{
System.Reflection.MemberInfo info = typeof(MyClass);
object[] attributes = info.GetCustomAttributes(true);
for (int i = 0; i < attributes.Length; i++)
{
System.Console.WriteLine(attributes[i]);
}
Console.ReadKey();

}
}
}

当上面的代码被编译和执行时,它会显示附加到类 MyClass 上的自定义特性:

HelpAttribute

实例

在本实例中,我们将使用在上一章中创建的 DeBugInfo 特性,并使用反射(Reflection)来读取 Rectangle 类中的元数据。

实例

using System;
using System.Reflection;
namespace BugFixApplication
{
// 一个自定义特性 BugFix 被赋给类及其成员
[AttributeUsage(AttributeTargets.Class |
AttributeTargets.Constructor |
AttributeTargets.Field |
AttributeTargets.Method |
AttributeTargets.Property,
AllowMultiple = true)]

public class DeBugInfo : System.Attribute
{
private int bugNo;
private string developer;
private string lastReview;
public string message;

public DeBugInfo(int bg, string dev, string d)
{
this.bugNo = bg;
this.developer = dev;
this.lastReview = d;
}

public int BugNo
{
get
{
return bugNo;
}
}
public string Developer
{
get
{
return developer;
}
}
public string LastReview
{
get
{
return lastReview;
}
}
public string Message
{
get
{
return message;
}
set
{
message = value;
}
}
}
[DeBugInfo(45, "Zara Ali", "12/8/2012",
Message = "Return type mismatch")]
[DeBugInfo(49, "Nuha Ali", "10/10/2012",
Message = "Unused variable")]
class Rectangle
{
// 成员变量
protected double length;
protected double width;
public Rectangle(double l, double w)
{
length = l;
width = w;
}
[DeBugInfo(55, "Zara Ali", "19/10/2012",
Message = "Return type mismatch")]
public double GetArea()
{
return length * width;
}
[DeBugInfo(56, "Zara Ali", "19/10/2012")]
public void Display()
{
Console.WriteLine("Length: {0}", length);
Console.WriteLine("Width: {0}", width);
Console.WriteLine("Area: {0}", GetArea());
}
}//end class Rectangle

class ExecuteRectangle
{
static void Main(string[] args)
{
Rectangle r = new Rectangle(4.5, 7.5);
r.Display();
Type type = typeof(Rectangle);
// 遍历 Rectangle 类的特性
foreach (Object attributes in type.GetCustomAttributes(false))
{
DeBugInfo dbi = (DeBugInfo)attributes;
if (null != dbi)
{
Console.WriteLine("Bug no: {0}", dbi.BugNo);
Console.WriteLine("Developer: {0}", dbi.Developer);
Console.WriteLine("Last Reviewed: {0}",
dbi.LastReview);
Console.WriteLine("Remarks: {0}", dbi.Message);
}
}

// 遍历方法特性
foreach (MethodInfo m in type.GetMethods())
{
foreach (Attribute a in m.GetCustomAttributes(true))
{
DeBugInfo dbi = (DeBugInfo)a;
if (null != dbi)
{
Console.WriteLine("Bug no: {0}, for Method: {1}",
dbi.BugNo, m.Name);
Console.WriteLine("Developer: {0}", dbi.Developer);
Console.WriteLine("Last Reviewed: {0}",
dbi.LastReview);
Console.WriteLine("Remarks: {0}", dbi.Message);
}
}
}
Console.ReadLine();
}
}
}

当上面的代码被编译和执行时,它会产生下列结果:

Length: 4.5
Width: 7.5
Area: 33.75
Bug No: 49
Developer: Nuha Ali
Last Reviewed: 10/10/2012
Remarks: Unused variable
Bug No: 45
Developer: Zara Ali
Last Reviewed: 12/8/2012
Remarks: Return type mismatch
Bug No: 55, for Method: GetArea
Developer: Zara Ali
Last Reviewed: 19/10/2012
Remarks: Return type mismatch
Bug No: 56, for Method: Display
Developer: Zara Ali
Last Reviewed: 19/10/2012
Remarks: 
AI 思考中...

3 篇笔记 写笔记

  1. #0

    错误代码:

    //遍历方法特性
    foreach (MethodInfo m in type.GetMethods())
    {
     foreach(Attribute a in m.GetCustomAttributes(true))
     {
     DeBugInfo dbi = (DeBugInfo)a;
     if(null!=dbi)
     {
     Console.WriteLine("Bug no: {0}, for Method: {1}",dbi.BugNo,m.Name);
     Console.WriteLine("Develop: {0}", dbi.Developer);
     Console.WriteLine("Last Reviewd:{0}", dbi.LastReview);
     Console.WriteLine("Reamrks: {0}", dbi.Message);
     }
     }
    }

    解决:将 (DeBugInfo)a 修改为 DeBugInfo dbi = a as DeBugInfo;

    原因:

    前者是一种强制转换类型,是一种将两个不同类型的值向上或者向下转换因此会报错。后者,通过 object 声明对象,是用了装箱和取消装箱的概念,也就是说 object 可以看成是所有类型的父类,因此 object 声明的对象可以转换成任意类型的值。

    6年前 (2020年05月23日)
  2. #0

    奇幻风之旅

    162***[email protected]

    394

    我凭着自己的理解给每段都加上了注释,特性+反射这里确实有点难理解。

    using System;
    using System.Reflection;//System.Reflection 类的 MemberInfo用于发现与类相关的特性(attribute)。
    namespace BugFixApplication
    {
     // 一个自定义特性 BugFix 被赋给类及其成员
     [AttributeUsage
     #region//定义了特性能被放在那些前面 
     (AttributeTargets.Class |//规定了特性能被放在class的前面
     AttributeTargets.Constructor |//规定了特性能被放在构造函数的前面
     AttributeTargets.Field |//规定了特性能被放在域的前面
     AttributeTargets.Method |//规定了特性能被放在方法的前面
     AttributeTargets.Property,//规定了特性能被放在属性的前面
     #endregion
     AllowMultiple = true)]//这个属性标记了我们的定制特性能否被重复放置在同一个程序实体前多次。
     public class DeBugInfo : System.Attribute//继承了预定义特性后的自定义特性
     {
     private int bugNo;
     private string developer;
     private string lastReview;
     public string message;
     public DeBugInfo(int bg,string dev,string d)//构造函数,接收三个参数并赋给对应实例
     {
     this.bugNo = bg;
     this.developer = dev;
     this.lastReview = d;
     }
     #region//定义对应的调用,返回对应值value
     public int BugNo
     {
     get 
     {
     return bugNo;
     }
     }
     public string Developer
     {
     get
     {
     return developer;
     }
     }
     public string LastReview
     {
     get
     {
     return lastReview;
     }
     }
     //前面有public string message;
     public string Message//定义了可以通过Message = "",来对message进行赋值。
     //因为不在构造函数中,所以是可选的
     {
     get
     {return message;}
     set
     {message = value;}
     }
     /*
     * 这部分可以简写如下
     * public string Message{get;set;}
     */
     }
     #endregion
     [DeBugInfo(45, "Zara Ali", "12/8/2012",
     Message = "Return type mismatch")]
     [DeBugInfo(49, "Nuha Ali", "10/10/2012",
     Message = "Unused variable")]//前面定义时的AllowMultiple=ture允许了多次使用在同一地方
     class Rectangle
     {
     protected double length;
     protected double width;//定义两个受保护的(封装)的成员变量
     public Rectangle(double l,double w)//构造函数,对两个成员变量进行初始化,公开的
     {
     length = l;
     width = w;
     }
     [DeBugInfo(55, "Zara Ali", "19/10/2012",
     Message = "Return type mismatch")]
     public double GetArea()
     {
     return length * width;
     }
     [DeBugInfo(56, "Zara Ali", "19/10/2012")]//因为message是可选项,所以可以不给出
     //不给出即为null,为空白
     public void Display()
     {
     Console.WriteLine("Length: {0}", length);
     Console.WriteLine("Width:{0}", width);
     Console.WriteLine("Area:{0}", GetArea());//常规打印
     }
     }
     class ExecuteRectangle
     {
     static void Main(string[] args)//程序入口
     {
     Rectangle r = new Rectangle(4.5, 7.5);//实例化
     r.Display();//执行打印长、宽、面积
     Type type = typeof(Rectangle);//让type对应这个Rectangle类
     // 遍历 Rectangle 类的特性
     foreach (Object attributes in type.GetCustomAttributes(false))//遍历Rectangle的所有特性
     {
     DeBugInfo dbi = (DeBugInfo)attributes;//强制转换
     if(null != dbi)//dbi非空
     {
     Console.WriteLine("Bug on: {0}", dbi.BugNo);
     Console.WriteLine("Developer: {0}", dbi.Developer);
     Console.WriteLine("Last REviewed: {0}", dbi.LastReview);
     Console.WriteLine("Remarks: {0}", dbi.Message);
     }
     }
     // 遍历方法特性
     foreach (MethodInfo m in type.GetMethods())//遍历Rectangle类下的所有方法
     {
     foreach (Attribute a in m.GetCustomAttributes(true))//遍历每个方法的特性
     {
     DeBugInfo dbi = a as DeBugInfo;//通过 object 声明对象,是用了装箱和取消装箱的概念.
     //也就是说 object 可以看成是所有类型的父类。
     //因此 object 声明的对象可以转换成任意类型的值。
     //通过拆装箱代替强制转换
     if (null !=dbi)//同理打印
     {
     Console.WriteLine("BugFixApplication no: {0},for Method: {1}", dbi.BugNo, m.Name);
     Console.WriteLine("Developer:{0}", dbi.Developer);
     Console.WriteLine("Last Reviewed: {0}", dbi.LastReview);
     Console.WriteLine("Remarks: {0}", dbi.Message);
     }
     }
     }
     Console.ReadKey();
     }
     }
    }

    奇幻风之旅

    162***[email protected]

    6年前 (2020年09月01日)
  3. #0

    fgrain

    281***[email protected]

    24

    补充一点:

    foreach (MethodInfo m in type.GetMethods())
    {
     foreach(Attribute a in m.GetCustomAttributes(true))
     {
     DeBugInfo dbi = (DeBugInfo)a;
     if(null!=dbi)
     {
     Console.WriteLine("Bug no: {0}, for Method: {1}",dbi.BugNo,m.Name);
     Console.WriteLine("Develop: {0}", dbi.Developer);
     Console.WriteLine("Last Reviewd:{0}", dbi.LastReview);
     Console.WriteLine("Reamrks: {0}", dbi.Message);
     }
     }
    }

    DeBugInfo dbi = (DeBugInfo)a 这句之所以会报错是因为遍历方法中的特性的时候遍历到了 bool Equals(object obj) 方法。

    这个方法是 Object 中的,使用 override 可以重写。当你查看这些方法的定义会发现 object 中的这些方法都有 [__DynamicallyInvokable] 特性。

    所以这里报错是因为 __DynamicallyInvokableDeBugInfo 之间没有继承关系所以无法强制转换,而使用as关键字会在转换失败的时候将 dbi 设为 null,可以尝试下去掉非空判断程序依然会报错。

    fgrain

    281***[email protected]

    5年前 (2022年01月14日)

点我分享笔记

  • 昵称 (必填)
  • 邮箱 (必填)
  • 引用地址

AltStyle によって変換されたページ (->オリジナル) /