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; // 属性名称匹配,不再继续内循环
                }
            }
        }
    }
}
相关推荐
Yorlen_Zhang8 分钟前
Python Tkinter Text 控件完全指南:从基础编辑器到富文本应用
开发语言·python·c#
lxl13079 分钟前
C++算法(1)双指针
开发语言·c++
不绝19120 分钟前
C#进阶:预处理指令/反射,Gettype,Typeof/关键类
开发语言·c#
无小道26 分钟前
Qt-qrc机制简单介绍
开发语言·qt
zhooyu33 分钟前
C++和OpenGL手搓3D游戏编程(20160207进展和效果)
开发语言·c++·游戏·3d·opengl
HAPPY酷36 分钟前
C++ 和 Python 的“容器”对决:从万金油到核武器
开发语言·c++·python
大鹏说大话36 分钟前
告别 MSBuild 脚本混乱:用 C# 和 Nuke 构建清晰、可维护的现代化构建系统
开发语言·c#
Mr_sun.1 小时前
Day09——入退管理-入住-2
android·java·开发语言
MAGICIAN...1 小时前
【java-软件设计原则】
java·开发语言
gpfyyds6661 小时前
Python代码练习
开发语言·python