C#高级:利用反射进行同名字段的映射(类似于AutoMap)

转移相同字段的数据信息

【需求】有两个列表A和B,调用方法后,A字段和B字段属性名字相同的数据,能相互传输(相当于EF的Map方法)。

【代码】

cs 复制代码
using System;
using System.Collections.Generic;
using System.Reflection;

public class EntityA
{
    public int Id { get; set; }         // 实体A的ID属性
    public string Name { get; set; }    // 实体A的Name属性
    public int Score { get; set; }      // 实体A的Score属性
    public string AA { get; set; }      // 实体A的AA属性
}

public class EntityB
{
    public int Id { get; set; }         // 实体B的ID属性
    public string Name { get; set; }    // 实体B的Name属性
    public int BB { get; set; }         // 实体B的BB属性
}

public class Program
{
    public static void Main()
    {
        // 创建两个实体列表
        List<EntityA> listA = new List<EntityA>
        {
            new EntityA { Id = 1, Name = "Alice", Score = 85, AA = "I like milk" },   // 初始化实体A列表
            new EntityA { Id = 2, Name = "Bob", Score = 72, AA = "I like apple" }
        };

        List<EntityB> listB = new List<EntityB>(); // 目标列表

        // 调用传输方法,将实体A列表转换为实体B列表
        TransferProperties(listA, listB);

        // 打印传输后的结果
        Console.WriteLine("List B after transfer:");
        foreach (var entity in listB)
        {
            Console.WriteLine($"Id: {entity.Id}, Name: {entity.Name}, BB: {entity.BB}");
        }
    }

    // 泛型方法,将实体A列表转换为实体B列表
    public static void TransferProperties<T, U>(List<T> listA, List<U> listB)
    {
        foreach (var source in listA)
        {
            // 创建目标对象U的实例
            U destination = Activator.CreateInstance<U>();

            // 将目标对象添加到目标列表
            listB.Add(destination);

            // 调用属性传输方法,将实体A的属性值复制到实体B中对应的属性
            TransferProperties(source, destination);
        }
    }

    // 泛型方法,将实体A的属性值复制到实体B中对应的属性
    public static void TransferProperties<T, U>(T source, U destination)
    {
        var sourceProperties = typeof(T).GetProperties();      // 获取实体A的所有属性
        var destinationProperties = typeof(U).GetProperties(); // 获取实体B的所有属性

        foreach (var sourceProperty in sourceProperties)
        {
            foreach (var destinationProperty in destinationProperties)
            {
                // 如果属性名称和类型匹配,并且目标属性可写,则复制属性值
                if (sourceProperty.Name == destinationProperty.Name &&
                    sourceProperty.PropertyType == destinationProperty.PropertyType &&
                    destinationProperty.CanWrite)
                {
                    var value = sourceProperty.GetValue(source);    // 获取源属性的值
                    destinationProperty.SetValue(destination, value); // 设置目标属性的值
                    break; // 属性名称匹配,不再继续内循环
                }
            }
        }
    }
}
相关推荐
MATLAB代码顾问5 小时前
5大智能算法优化标准测试函数对比(Python实现)
开发语言·python
工程师0076 小时前
C# 装箱、拆箱 底层原理
c#·装箱和拆箱
万粉变现经纪人7 小时前
如何解决 pip install llama-cpp-python 报错 未安装 CMake/Ninja 或 CPU 不支持 AVX 问题
开发语言·python·开源·aigc·pip·ai写作·llama
清风明月一壶酒7 小时前
OpenClaw自动处理Word文档全流程
开发语言·c#·word
其实防守也摸鱼7 小时前
CTF密码学综合教学指南--第五章
开发语言·网络·笔记·python·安全·网络安全·密码学
小郑加油8 小时前
python学习Day12:pandas安装与实际运用
开发语言·python·学习
AC赳赳老秦8 小时前
投标合规提效:用 OpenClaw 实现标书 / 合同自动审核、关键词校验、格式优化,降低废标风险
开发语言·前端·python·eclipse·emacs·deepseek·openclaw
kyriewen8 小时前
代码写成一锅粥?3个设计模式让你的项目“起死回生”
前端·javascript·设计模式
不会敲代码19 小时前
从零搭建 AI 日记助手:用 Milvus 向量数据库实现语义搜索
javascript·openai
KuaCpp9 小时前
C++面向对象(速过复习版)
开发语言·c++