(四十一)C#编程基础复习——C#反射(Reflection)
反射(Reflection)是指程序可以访问、检测和修改它本身状态或行为的一种能力,反射中提供了用来描述程序集、模块和类型的对象,可以使用反射动态地创建类型的实例,并将类型绑定到现有对象,或者从现有对象中获取类型,然后调用其方法或访问其字段和属性。如果代码中使用了特性,也可以利用放射来访问它们。
一、反射的用途
C#中反射具有以下用途:
- 在运行时查看视图属性信息;
- 检查装配中的各种类型并实例化这些类型;
- 在后期绑定到方法和属性;
- 在运行时创建新类型,然后使用这些类型执行一些任务。
二、查看元数据
前面我们提供了可以使用反射查看特性的信息,下面来看看具体操作。首先需要初始化System.Reflection类的MemberInfo对象,用来发现与类关联的属性。例如:System.Reflection.MemberInfo info=type(MyClass);
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;
[AttributeUsage(AttributeTargets.All)]
public class HelpAttribute : System.Attribute
{
public readonly string Url;
public string Topic //Topic是一个name_parameter
{
get
{
return Topic;
}
set
{
Topic = value;
}
}
public HelpAttribute(string url) //url是一个positional_parameters
{
this.Url = url;
}
private string topic;
}
[HelpAttribute("关于MyClass的信息")]
class MyClass
{
}
namespace _044
{
internal class Program
{
static void Main(string[] args)
{
System.Reflection.MemberInfo info = typeof(MyClass);
object[] attributers = info.GetCustomAttributes(true);
for(int i=0;i<attributers.Length;i++)
{
System.Console.WriteLine(attributers[i]);
}
Console.ReadKey();
}
}
}
三、使用在上一节中创建的DeBugInfo特性,并使用反射(Reflection)来读取举行类中的元数据。
using System;
using System.Reflection;
namespace c.biancheng.net
{
// 要分配给类及其成员的自定义属性错误修复程序
[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 = "返回值类型不匹配")]
[DeBugInfo(49, "Nuha Ali", "10/10/2012", Message = "变量未使用")]
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 = "返回值类型不匹配")]
public double GetArea()
{
return length * width;
}
[DeBugInfo(56, "Zara Ali", "19/10/2012")]
public void Display()
{
Console.WriteLine("长: {0}", length);
Console.WriteLine("宽: {0}", width);
Console.WriteLine("面积: {0}", GetArea());
}
}
class Demo
{
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 编号: {0}", dbi.BugNo);
Console.WriteLine("开发者: {0}", dbi.Developer);
Console.WriteLine("上次审核时间: {0}", dbi.LastReview);
Console.WriteLine("评论: {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 编号: {0}, 函数名: {1}", dbi.BugNo, m.Name);
Console.WriteLine("开发者: {0}", dbi.Developer);
Console.WriteLine("上次审核时间: {0}", dbi.LastReview);
Console.WriteLine("评论: {0}", dbi.Message);
}
}
}
Console.ReadLine();
}
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了