C# 反射 (Reflection) 的常用功能

目录

一、概述

二、实例化类

三、反射赋值

四、获取字段值

五、获取字段名

六、获取字段类型

七、调用方法

结束


一、概述

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

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

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


优点:

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


缺点:

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


反射(Reflection)有下列用途:

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

二、实例化类

要想使用反射来实例化一个类,可以使用 Activator.CreateInstance 方法,但实际上,C# 的反射性能要比 new 差很多,所以在一般情况下,不推荐使用反射,但是也不代表反射不能使用,在很多项目中,反射用到的频率也是很高的。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ReflectionTest
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Type type = typeof(MyClass);
            object obj = Activator.CreateInstance(type);
            MyClass myClass = (MyClass)obj;
            Console.WriteLine("年龄:{0}", myClass.Age);

            Console.ReadKey();
        }
    }

    public class MyClass
    {
        public int Age { get; set; } = 12;
    }
}

运行:

三、反射赋值

1.字段类型

可以使用 FieldInfo.SetValue 来给字段赋值

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace ReflectionTest
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Type type = typeof(MyClass);
            object obj = Activator.CreateInstance(type);

            FieldInfo field = type.GetField("Age");
            if (field != null)
                field.SetValue(obj, 42);

            MyClass myClass = (MyClass)obj;
            Console.WriteLine("年龄:{0}", myClass.Age);

            Console.ReadKey();
        }
    }

    public class MyClass
    {
        public int Age = 12;
    }
}

运行:

2.属性类型

根据上一节的代码可以看到,我把 Age 这个字段的属性去掉了,如果字段是一个属性, 调用 type.GetField("字段名") 方法返回的是 null。

下面是介绍如何给属性赋值:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace ReflectionTest
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Type type = typeof(MyClass);
            object obj = Activator.CreateInstance(type);
            PropertyInfo property = type.GetProperty("UserName");
            property.SetValue(obj, "李四");
            MyClass myClass = (MyClass)obj;
            Console.WriteLine("用户名:{0}", myClass.UserName);

            Console.ReadKey();
        }
    }

    public class MyClass
    {
        public int Age = 12;

        public string UserName { get; set; } = "张三";
    }
}

运行:

四、获取字段值

获取字段的值,只需要拿到当前类的实体对象就好了

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace ReflectionTest
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Type type = typeof(MyClass);
            object obj = Activator.CreateInstance(type);
        
            MyClass myClass = (MyClass)obj;
            Console.WriteLine("年龄:{0}", myClass.Age);
            Console.WriteLine("用户名:{0}", myClass.UserName);

            Console.ReadKey();
        }
    }

    public class MyClass
    {
        public int Age = 12;

        public string UserName { get; set; } = "张三";
    }
}

运行:

五、获取字段名

1.字段类型

可以使用下面的方法来获取所有的字段名,但这种写法并不能获取属性的字段名

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace ReflectionTest
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Type type = typeof(MyClass);
            FieldInfo[] fields = type.GetFields(BindingFlags.Public | BindingFlags.Instance);
            foreach (FieldInfo field in fields)
            {
                Console.WriteLine(field.Name);
            }

            Console.ReadKey();
        }
    }

    public class MyClass
    {
        public int Age = 12;

        public string UserName { get; set; } = "张三";
    }
}

运行:

2.属性类型

代码大致差不多,区别在于,普通的字段使用 GetFields ,属性使用  GetProperties

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace ReflectionTest
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Type type = typeof(MyClass);
            PropertyInfo[] properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
            foreach (PropertyInfo property in properties)
            {
                Console.WriteLine(property.Name);
            }

            Console.ReadKey();
        }
    }

    public class MyClass
    {
        public int Age = 12;

        public string UserName { get; set; } = "张三";
    }
}

运行:

六、获取字段类型

1.字段类型

可以使用 FieldInfo.FieldType 来获取字段的类型

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace ReflectionTest
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Type type = typeof(MyClass);
            FieldInfo field = type.GetField("Age");
            Type fieldType = field.FieldType;
            Console.WriteLine(fieldType);

            Console.ReadKey();
        }
    }

    public class MyClass
    {
        public int Age = 12;
        public string UserName { get; set; } = "张三";
    }
}

运行:

2.属性类型

可以使用 PropertyInfo.GetProperty 方法来获取属性的类型

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace ReflectionTest
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Type type = typeof(MyClass);
            PropertyInfo property = type.GetProperty("UserName");
            Type propertyType = property.PropertyType;
            Console.WriteLine(propertyType);

            Console.ReadKey();
        }
    }

    public class MyClass
    {
        public int Age = 12;
        public string UserName { get; set; } = "张三";
    }
}

运行:

七、调用方法

可以使用 MethodInfo.Invoke 来调用方法

1.有参数

在这里,参数用的是一个数组,假设参数有两个,那么你在数组中也要加两个对应类型的值

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace ReflectionTest
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Type type = typeof(MyClass);
            object obj = Activator.CreateInstance(type);
            MethodInfo method = type.GetMethod("Test");
            object[] parameters = new object[] { "老王" };
            string result = (string)method.Invoke(obj, parameters);
            Console.WriteLine("返回值 {0}" , result);

            Console.ReadKey();
        }
    }

    public class MyClass
    {
        public string Test(string name)
        {
            return "你的名字:" + name;
        }
    }
}

运行:

2.无参数

假设方法无参数的时候,传值传空就好了

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace ReflectionTest
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Type type = typeof(MyClass);
            object obj = Activator.CreateInstance(type);
            MethodInfo method = type.GetMethod("SayHi");
            method.Invoke(obj, null);

            Console.ReadKey();
        }
    }

    public class MyClass
    {
        public void SayHi()
        {
            Console.WriteLine("你好");
        }
    }
}

运行:

结束

如果这个帖子对你有所帮助,欢迎 关注 + 点赞 + 留言,谢谢!

end

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
C# 反射是指在运行时动态地获取类的信息,通过反射可以获取类的属性、方法、事件等信息,以及动态创建对象、调用方法、获取等操作。这使得我们能够在运行时动态地编写代码,可以写出更加灵活和可扩展的程序。 C# 反射的核心是 `System.Reflection` 命名空间,该命名空间包含了许多与反射相关的类和接口,比如 `Type` 类、`MethodInfo` 类、`PropertyInfo` 类等。 下面是一些常用反射操作: 1. 获取类型信息 可以使用 `typeof` 关键字或者 `Type.GetType()` 方法来获取类型的信息。`typeof` 关键字用于获取编译时已知的类型信息,而 `Type.GetType()` 方法则可以通过类型名称获取运行时的类型信息。 ```csharp // 获取 System.String 类型的信息 Type type1 = typeof(System.String); // 获取指定类型名称的信息 Type type2 = Type.GetType("System.String"); ``` 2. 获取成员信息 可以使用 `Type.GetMembers()` 方法获取类型的所有成员信息,包括属性、方法、字段、事件等。也可以使用 `Type.GetMethod()`、`Type.GetProperty()`、`Type.GetField()`、`Type.GetEvent()` 等方法获取指定成员的信息。 ```csharp Type type = typeof(Person); // 获取类型的所有成员信息 MemberInfo[] members = type.GetMembers(); // 获取指定属性的信息 PropertyInfo property = type.GetProperty("Name"); // 获取指定方法的信息 MethodInfo method = type.GetMethod("SayHello"); // 获取指定字段的信息 FieldInfo field = type.GetField("Age"); // 获取指定事件的信息 EventInfo eventInfo = type.GetEvent("PropertyChanged"); ``` 3. 动态创建对象 可以使用 `Activator.CreateInstance()` 方法动态创建对象,也可以使用 `Type.InvokeMember()` 方法调用构造函数来创建对象。 ```csharp Type type = typeof(Person); // 使用 Activator.CreateInstance() 方法创建对象 Person person1 = (Person)Activator.CreateInstance(type); // 使用 Type.InvokeMember() 方法调用构造函数创建对象 Person person2 = (Person)type.InvokeMember(null, BindingFlags.CreateInstance, null, null, new object[] { "Tom", 18 }); ``` 4. 调用成员 可以使用 `MethodInfo.Invoke()` 方法调用方法,也可以使用 `PropertyInfo.SetValue()` 方法设置属性,使用 `FieldInfo.SetValue()` 方法设置字段的。 ```csharp Type type = typeof(Person); Person person = new Person("Tom", 18); // 调用方法 MethodInfo method = type.GetMethod("SayHello"); method.Invoke(person, null); // 设置属性 PropertyInfo property = type.GetProperty("Name"); property.SetValue(person, "Jerry", null); // 设置字段的 FieldInfo field = type.GetField("Age"); field.SetValue(person, 20); ``` 以上是 C# 反射的一些基本操作,反射的应用非常广泛,可以用来实现插件式开发、ORM 映射等功能
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

熊思宇

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

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

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

打赏作者

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

抵扣说明:

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

余额充值