.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}");
    }
}
相关推荐
FL16238631294 小时前
VTK源码编译时候选qt5路径
开发语言·qt
Felven4 小时前
C. Maximum Median
c语言·开发语言·算法
CryptoRzz4 小时前
StockTV API 对接全攻略(股票、期货、IPO)
java·javascript·git·web3·区块链·github
iReachers4 小时前
为什么HTML打包安卓APP安装时会覆盖或者报错?
android·java·html·html打包apk·网页打包
纟 冬4 小时前
Flutter & OpenHarmony 运动App运动模式选择组件开发
android·java·flutter
Wang's Blog4 小时前
Lua: 基于协程的生产者-消费者模型实现
开发语言·lua
毕设源码-赖学姐5 小时前
【开题答辩全过程】以 基于Springboot的智慧养老系统的设计与实现为例,包含答辩的问题和答案
java·spring boot·后端
jamesge20105 小时前
限流之漏桶算法
java·开发语言·算法
jvstar5 小时前
JAVA面试题和答案
java