.net 浅复制

你可以使用C#编程语言来编写一个通用的扩展方法,用于将一个对象的值复制到另一个对象,并且修改目标对象的属性时原始对象不受影响。

以下是一个示例代码:

cs 复制代码
       public static T ShallowCopy<T>(this T original) where T : class
        {
            if (original == null)
            {
                return null;
            }

            // 创建一个新实例
            T copy = Activator.CreateInstance<T>();

            // 获取原始对象的所有属性
            var properties = typeof(T).GetProperties();

            foreach (var property in properties)
            {
                // 如果属性是一个引用类型或是List集合,进行浅拷贝
                if (property.PropertyType.IsClass && property.PropertyType != typeof(string)
                    || property.PropertyType.IsGenericType && property.PropertyType.GetGenericTypeDefinition() == typeof(List<>))
                {
                    var originalValue = property.GetValue(original);

                    if (originalValue != null)
                    {
                        if (property.PropertyType.IsGenericType && property.PropertyType.GetGenericTypeDefinition() == typeof(List<>))
                        {
                            // 如果属性是List集合,复制集合元素
                            var originalList = (System.Collections.IList)originalValue;
                            var copyList = (System.Collections.IList)Activator.CreateInstance(property.PropertyType);

                            foreach (var item in originalList)
                            {
                                copyList.Add(item);
                            }
                            property.SetValue(copy, copyList);

                             如果属性是List集合,复制集合元素
                            //var originalList = (System.Collections.IList)originalValue;
                            //var copyList = originalList.Cast<object>().ToList();

                            //property.SetValue(copy, copyList);
                        }
                        else
                        {
                            // 其他引用类型的属性,进行递归浅拷贝
                            var clonedObject = ShallowCopy(originalValue);
                            property.SetValue(copy, clonedObject);
                        }
                    }
                }
                else
                {
                    // 该属性是一个值类型,直接复制
                    var originalValue = property.GetValue(original);
                    property.SetValue(copy, originalValue);
                }
            }

            return copy;
        }

可以按照以下方式使用该扩展方法:

cs 复制代码
public class A
{
    public int Foo { get; set; }
    public string Bar { get; set; }
}

public class B
{
    public int Foo { get; set; }
    public string Bar { get; set; }
}

public class Program
{
    static void Main()
    {
        A a = new A { Foo = 42, Bar = "Hello" };
        B b = new B();

        b=a.ShallowCopy();

        Console.WriteLine($"a: Foo = {a.Foo}, Bar = {a.Bar}");
        Console.WriteLine($"b: Foo = {b.Foo}, Bar = {b.Bar}");
        
        b.Foo = 100; // 修改b对象的属性值
        
        Console.WriteLine($"a: Foo = {a.Foo}, Bar = {a.Bar}");
        Console.WriteLine($"b: Foo = {b.Foo}, Bar = {b.Bar}");
    }
}
相关推荐
小bo波6 小时前
Java Swing 图形用户界面实验 —— 从算术练习到游戏开发的完整实践
java·课程设计·gui·游戏开发·扫雷·swing
咖啡八杯8 小时前
GoF设计模式——备忘录模式
java·后端·spring·设计模式
SamDeepThinking18 小时前
裁掉那个差程序员后,给你看团队里高手的代码:这个习惯,希望你有
java·后端·程序员
朕瞧着你甚好19 小时前
技术雷达 & Java 集成评估报告 — Apache Tika 3.3.1
java·ai编程
MacroZheng20 小时前
短短几天,暴涨2.8万Star!又一款编程神器开源!
java·人工智能·后端
SamDeepThinking20 小时前
函数式编程:用BiFunction消除多类型分支的代码重复
java·后端·面试
Flittly2 天前
【AgentScope Java新手村系列】(16)从RAG到多路检索
java·spring boot·spring
小兔崽子去哪了2 天前
Java 生成二维码解决方案
java·后端
人活一口气2 天前
从JVM调优到MCP协议:Java全栈技术体系深度总结与企业级架构实践
java·spring boot
NE_STOP2 天前
Vibe Coding -- 完整项目案例实操
java