C# | 通过一个简单的例子,理解反射

1 概述

在程序运行阶段,如果需要操作对象的元数据,这时,就会用到反射。看下面的linq 语句:

var query = from m in typeof(string).GetMethods()
            where m.IsStatic == true
            orderby m.Name
            group m by m.Name into g
            orderby g.Count()
            select new { Name = g.Key,
            Overloads = g.Count()};

            foreach(var item in query)
            {
                Console.WriteLine(item);
            }

通过反射,我们可以知道对象里有多少方法,甚至有多少方法被重载。

2 举个例子

反射可以用到测试场景中,比如下面的代码:

Type personType = typeof(Person);

var properties = personType.GetProperties();

foreach (var item in properties)
{
    Console.WriteLine($"Property: Type: {item.PropertyType.Name}
            | Name: {item.Name}");
}

Person person = new Person();

var methods = personType.GetMethods();
foreach (var item in methods)
{
    Console.WriteLine($"Method: Type: {item.ReturnType.Name}
            | Name {item.Name}");

    if (item.Name == "Print")
    {
        item.Invoke(person, null);
    }
}
    Console.WriteLine("-------------------------------");
    AttributeTest(typeof(Person));

    Console.ReadKey();

通过 Type 类和typeof 方法,创建一个Person 实例。然后,调用 GetMethod 方法,获取对象的所有公共方法。

我们再创建一个自定义的attribute

public class RunMethodAttribute : Attribute
{
    public int Count { get; set; }
}

现在,可以使用自定义的attribute,设置值后,在反射调用中使用。

[RunMethod(Count = 3)]
public void Print()
{
    Console.WriteLine($"{FirstName} {LastName}");
}

[RunMethod(Count = 3)]
public void TestMethod()
{
    Console.WriteLine("Hello from TestMethod");
}

public void Move(int newZipCode)
{
    ZipCode = newZipCode;
    Console.WriteLine($"{FirstName} {LastName} has been moved to {newZipCode}");
}

[RunMethod(Count = 1)]
public void SayHi()
{
    Console.WriteLine($"Hi {FirstName}");
}

这种方法和文章开头的处的linq语句类似,我们还可以通过GetMethod 或者 GetCustomAttribute 方法来调用喜欢的方法类型。

static void AttributeTest(Type type)
{
    // Get the methods
    var allMethods = type.GetMethods();
    var methodsWithAttribute = allMethods.Where(m => m.GetCustomAttribute(typeof(RunMethodAttribute)) != null);

    var obj = Activator.CreateInstance(type);

    foreach (var item in methodsWithAttribute)
    {
        var attribute = (RunMethodAttribute)item.GetCustomAttribute(typeof(RunMethodAttribute));
        Console.WriteLine($"{item.Name} run for {attribute.Count} times");
        for (int i = 0; i < attribute.Count; i++)
        {
            item.Invoke(obj, null);
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

孟华328

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值