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);
        }
    }
}

猜你喜欢

转载自blog.csdn.net/alexwei2009/article/details/125351768