当前位置: 首页 > article >正文

C#的反射使用示例

C# 反射机制提供了在运行时动态获取类型信息、创建对象、调用方法等能力:

1. 插件式架构

在插件式架构中,主程序需要在运行时动态加载插件程序集,并调用其中的类型和方法。反射可以帮助实现这种动态加载和调用的功能。

示例代码
  • 插件接口定义(PluginInterface.cs)
namespace PluginInterface
{
    public interface IPlugin
    {
        void Execute();
    }
}
  • 插件实现(Plugin1.cs)
using PluginInterface;

namespace Plugin1
{
    public class MyPlugin : IPlugin
    {
        public void Execute()
        {
            System.Console.WriteLine("Plugin1 is executed.");
        }
    }
}
  • 主程序(MainProgram.cs)
using System;
using System.IO;
using System.Reflection;
using PluginInterface;

class MainProgram
{
    static void Main()
    {
        // 加载插件程序集
        Assembly assembly = Assembly.LoadFrom("Plugin1.dll");

        // 获取实现 IPlugin 接口的类型
        foreach (Type type in assembly.GetTypes())
        {
            if (typeof(IPlugin).IsAssignableFrom(type) && !type.IsInterface)
            {
                // 创建插件实例
                IPlugin plugin = (IPlugin)Activator.CreateInstance(type);
                // 调用插件方法
                plugin.Execute();
            }
        }
    }
}

2. 配置文件驱动的对象创建

当需要根据配置文件中的类型信息动态创建对象时,反射可以发挥作用。

示例代码
  • 配置文件(config.txt)

plaintext

MyNamespace.MyClass
  • 类定义(MyClass.cs)
namespace MyNamespace
{
    public class MyClass
    {
        public void DoSomething()
        {
            System.Console.WriteLine("MyClass is doing something.");
        }
    }
}
  • 主程序(MainProgram.cs)
using System;
using System.IO;
using System.Reflection;

class MainProgram
{
    static void Main()
    {
        // 从配置文件中读取类型名称
        string typeName = File.ReadAllText("config.txt").Trim();

        // 获取类型
        Type type = Type.GetType(typeName);
        if (type != null)
        {
            // 创建对象实例
            object obj = Activator.CreateInstance(type);
            // 获取方法信息
            MethodInfo method = type.GetMethod("DoSomething");
            if (method != null)
            {
                // 调用方法
                method.Invoke(obj, null);
            }
        }
    }
}

3. 数据映射和转换

在数据处理中,可能需要将一个对象的属性值映射到另一个对象的属性上。反射可以帮助实现这种动态的数据映射。

示例代码
using System;
using System.Reflection;

class SourceClass
{
    public string Name { get; set; }
    public int Age { get; set; }
}

class TargetClass
{
    public string Name { get; set; }
    public int Age { get; set; }
}

class Program
{
    static void Main()
    {
        SourceClass source = new SourceClass { Name = "John", Age = 30 };
        TargetClass target = new TargetClass();

        // 使用反射进行属性映射
        Type sourceType = source.GetType();
        Type targetType = target.GetType();

        foreach (PropertyInfo sourceProperty in sourceType.GetProperties())
        {
            PropertyInfo targetProperty = targetType.GetProperty(sourceProperty.Name);
            if (targetProperty != null && targetProperty.CanWrite &&
                sourceProperty.PropertyType == targetProperty.PropertyType)
            {
                targetProperty.SetValue(target, sourceProperty.GetValue(source));
            }
        }

        Console.WriteLine($"Name: {target.Name}, Age: {target.Age}");
    }
}
具体分析
  1. sourceType.GetProperties():这行代码会获取 SourceClass 类型的所有公共属性。对于 SourceClass 来说,它有两个公共属性 Name 和 Age

  2. foreach (PropertyInfo sourceProperty in sourceType.GetProperties()):这个 foreach 循环会依次遍历 SourceClass 的每个属性。在每次循环中,sourceProperty 代表当前正在处理的属性的信息,它是一个 PropertyInfo 类型的对象。

  3. sourceProperty.NamePropertyInfo 类的 Name 属性用于获取属性的名称。当第一次循环时,sourceProperty 可能代表 Name 属性,此时 sourceProperty.Name 的值就是 "Name";当第二次循环时,sourceProperty 代表 Age 属性,sourceProperty.Name 的值就是 "Age"

  4. targetType.GetProperty(sourceProperty.Name):这行代码会根据 sourceProperty.Name 的值,在 TargetClass 类型中查找同名的属性。如果找到了同名属性,就将其赋值给 targetProperty


http://www.kler.cn/a/520754.html

相关文章:

  • c++小知识点
  • 从规则到神经网络:机器翻译技术的演进与未来展望
  • Golang 执行流程分析
  • 「 机器人 」扑翼飞行器的偏航力矩控制:分周期参数调节机制
  • 【SpringMVC】——Json数据交互处理
  • Leetcode::3432. 统计元素和差值为偶数的分区方案
  • 数据库、数据仓库、数据湖有什么不同
  • redis 实践与扩展
  • 【论文复现】一种改进哈里斯鹰优化算法用于连续和离散优化问题
  • SSM开发(三) spring与mybatis整合(含完整运行demo源码)
  • STM32 OLED屏配置
  • 新电脑第一次开机激活
  • 基于OpenCV实现的答题卡自动判卷系统
  • 【机器学习】深入探索SVM:支持向量机的原理与应用
  • 三角形的最大周长(LeetCode 976)
  • 项目测试之Jmeter
  • 第27篇 基于ARM A9处理器用C语言实现中断<三>
  • 配电自动化系统“三区四层”数字化架构
  • HTML<hgroup>标签
  • 【HuggingFace项目】:Open-R1 - DeepSeek-R1 大模型开源复现计划