C#将所有对象参数复制到子类对象

C#将所有对象参数复制到子类对象,c#,copy-constructor,C#,Copy Constructor,假设我有一个不能更改的父类的对象,例如,ListBox的一个实例,它有一长串参数。现在我创建一个子类: class PlaylistBox : ListBox { void CopySettingsFrom(ListBox In) { //...what now? } } 问题-如何有效地将对象中的复制到播放框的新对象中 您可以使用反射 //Other Imports... using System.Reflection; public Playl

假设我有一个不能更改的父类的对象,例如,
ListBox
的一个实例,它有一长串参数。现在我创建一个子类:

class PlaylistBox : ListBox
{

    void CopySettingsFrom(ListBox In)
    {
         //...what now?
    }
}
问题-如何有效地将
对象中的
复制到
播放框的新对象中

您可以使用反射

//Other Imports...
using System.Reflection;

public PlaylistBox(ListBox In)
{
    PropertyInfo[] properties = typeof(ListBox).GetProperties();

    foreach (PropertyInfo p in properties)
        if (p.CanRead && p.CanWrite)
            p.SetMethod.Invoke(this, new object[] { p.GetMethod.Invoke(In, null) });
}

对于.NET<4.5,分别用对和方法的调用替换对和属性的调用。

这里是一个基于反射和AutoMapper的3种方法的示例,并附有说明:

internal class Program
    {
        private static void Main(string[] args)
        {
            Example1();
            Example2();
            Example3();
        }

        public static void Example1()
        {
            Console.WriteLine("This example shows using copy with reflection. Minus of this method - u have to implement FULL copy for each class or u will copy only references to object properties");

            //creating new parent class with some values
            var parentClass = new ParentClass
            {
                Property1 = "qqq",
                Property2 = 1,
                ObjectProperty = new SomeClassWithObjectProperty
                {
                    ObjectProperty = new SomeObjectClass {SomeProperty = "www"}
                }
            };

            //crating new child class and copy REFERENCES to properties
            var childClassReflection = new ChildClassReflection(parentClass);

            //changing properties of parent
            parentClass.Property1 = "rrr";
            parentClass.Property2 = 2;
            parentClass.ObjectProperty.ObjectProperty.SomeProperty = "eee";

            //we will get OLD values for VALUE types and OLD values for REFERENCE types
            //qqq 1 WWW
            Console.WriteLine(childClassReflection.Property1 + " " + childClassReflection.Property2 + " " + childClassReflection.ObjectProperty.ObjectProperty.SomeProperty);
        }

        public static void Example2()
        {
            Console.WriteLine();
            Console.WriteLine("This example shows using copy with reflection WITH FULL COPY");

            //creating new parent class with some values
            var parentClass = new ParentClass
            {
                Property1 = "qqq",
                Property2 = 1,
                ObjectProperty = new SomeClassWithObjectProperty
                {
                    ObjectProperty = new SomeObjectClass {SomeProperty = "www"}
                }
            };

            //crating new child class and copy REFERENCES to properties
            var childClassReflection = new ChildClassReflectionWithFullCopy(parentClass);

            //changing properties of parent
            parentClass.Property1 = "rrr";
            parentClass.Property2 = 2;
            parentClass.ObjectProperty.ObjectProperty.SomeProperty = "eee";

            //we will get OLD values for VALUE types and NEW values for REFERENCE types
            //qqq 1 eee
            Console.WriteLine(childClassReflection.Property1 + " " + childClassReflection.Property2 + " " + childClassReflection.ObjectProperty.ObjectProperty.SomeProperty);
        }

        public static void Example3()
        {
            //here i will show copy using AutoMapper
            Console.WriteLine();
            Console.WriteLine("This example shows using copy with AutoMapper");

            //creating new parent class with some values
            var parentClass = new ParentClass
            {
                Property1 = "qqq",
                Property2 = 1,
                ObjectProperty = new SomeClassWithObjectProperty
                {
                    ObjectProperty = new SomeObjectClass { SomeProperty = "www" }
                }
            };

            Mapper.Initialize(cfg => cfg.CreateMap<ParentClass, ChildClassAutoMapper>());           

            //crating new child class and copy REFERENCES to properties
            var childClassReflection = Mapper.Map<ChildClassAutoMapper>(parentClass);

            //changing properties of parent
            parentClass.Property1 = "rrr";
            parentClass.Property2 = 2;
            parentClass.ObjectProperty.ObjectProperty.SomeProperty = "eee";

            //we will get OLD values for VALUE types and OLD values for REFERENCE types
            //qqq 1 eee
            Console.WriteLine(childClassReflection.Property1 + " " + childClassReflection.Property2 + " " + childClassReflection.ObjectProperty.ObjectProperty.SomeProperty);
        }
    }

    public class ChildClassAutoMapper:ParentClass
    {       
    }

    public class ChildClassReflection : ParentClass
    {
        public ChildClassReflection(ParentClass parentClass)
        {
            foreach (var p in ParentProperties)
                p.SetMethod.Invoke(this, new[] {p.GetMethod.Invoke(parentClass, null)});
        }

        //do it only once for best performance
        private static PropertyInfo[] ParentProperties { get; } = typeof(ParentClass).GetProperties().Where(c => c.CanRead && c.CanWrite).ToArray();
    }

    public class ChildClassReflectionWithFullCopy : ParentClass
    {
        public ChildClassReflectionWithFullCopy(ParentClass parentClass)
        {
            var parentClassLocal = JsonConvert.DeserializeObject<ParentClass>(JsonConvert.SerializeObject(parentClass));
            foreach (var p in ParentProperties)
                p.SetMethod.Invoke(this, new[] {p.GetMethod.Invoke(parentClassLocal, null)});
        }

        //do it only once for best performance
        private static PropertyInfo[] ParentProperties { get; } = typeof(ParentClass).GetProperties().Where(c => c.CanRead && c.CanWrite).ToArray();
    }

    public class ParentClass
    {
        public string Property1 { get; set; }
        public int Property2 { get; set; }
        public SomeClassWithObjectProperty ObjectProperty { get; set; }
    }

    public class SomeClassWithObjectProperty
    {
        public SomeObjectClass ObjectProperty { get; set; }
    }

    public class SomeObjectClass
    {
        public string SomeProperty { get; set; }
    }
内部类程序
{
私有静态void Main(字符串[]args)
{
例1();
例2();
例3();
}
公共静态无效示例1()
{
WriteLine(“本例显示使用带反射的copy.减号方法-必须为每个类实现完整复制,否则将只复制对对象属性的引用”);
//使用一些值创建新的父类
var parentClass=新的父类
{
Property1=“qqq”,
属性2=1,
ObjectProperty=新建SomeClassWithObjectProperty
{
ObjectProperty=newsomeobjectclass{SomeProperty=“www”}
}
};
//装入新的子类并复制对属性的引用
var childClassReflection=新的childClassReflection(父类);
//更改父对象的属性
parentClass.Property1=“rrr”;
parentClass.Property2=2;
parentClass.ObjectProperty.ObjectProperty.SomeProperty=“eee”;
//我们将获得值类型的旧值和引用类型的旧值
//QQ1 WWW
Console.WriteLine(childClassReflection.Property1+“”+childClassReflection.Property2+“”+childClassReflection.ObjectProperty.ObjectProperty.SomeProperty);
}
公共静态无效示例2()
{
Console.WriteLine();
WriteLine(“此示例显示使用带有反射的复制和完整复制”);
//使用一些值创建新的父类
var parentClass=新的父类
{
Property1=“qqq”,
属性2=1,
ObjectProperty=新建SomeClassWithObjectProperty
{
ObjectProperty=newsomeobjectclass{SomeProperty=“www”}
}
};
//装入新的子类并复制对属性的引用
var childClassReflection=new ChildClassReflectionWithFullCopy(parentClass);
//更改父对象的属性
parentClass.Property1=“rrr”;
parentClass.Property2=2;
parentClass.ObjectProperty.ObjectProperty.SomeProperty=“eee”;
//我们将获得值类型的旧值和引用类型的新值
//QQ1EEE
Console.WriteLine(childClassReflection.Property1+“”+childClassReflection.Property2+“”+childClassReflection.ObjectProperty.ObjectProperty.SomeProperty);
}
公共静态无效示例3()
{
//在这里,我将使用AutoMapper显示副本
Console.WriteLine();
WriteLine(“此示例显示使用带有AutoMapper的copy”);
//使用一些值创建新的父类
var parentClass=新的父类
{
Property1=“qqq”,
属性2=1,
ObjectProperty=新建SomeClassWithObjectProperty
{
ObjectProperty=newsomeobjectclass{SomeProperty=“www”}
}
};
初始化(cfg=>cfg.CreateMap());
//装入新的子类并复制对属性的引用
var childClassReflection=Mapper.Map(parentClass);
//更改父对象的属性
parentClass.Property1=“rrr”;
parentClass.Property2=2;
parentClass.ObjectProperty.ObjectProperty.SomeProperty=“eee”;
//我们将获得值类型的旧值和引用类型的旧值
//QQ1EEE
Console.WriteLine(childClassReflection.Property1+“”+childClassReflection.Property2+“”+childClassReflection.ObjectProperty.ObjectProperty.SomeProperty);
}
}
公共类ChildClassAutoMapper:父类
{       
}
公共类ChildClassReflection:父类
{
公共子类反射(父类父类)
{
foreach(ParentProperties中的var p)
p、 调用(这是新的[]{p.GetMethod.Invoke(parentClass,null)});
}
//只做一次,以获得最佳性能
私有静态属性info[]ParentProperties{get;}=typeof(ParentClass).GetProperties()。其中(c=>c.CanRead&&c.CanWrite).ToArray();
}
公共类ChildClassReflectionWithFullCopy:ParentClass
{
公共ChildClass ReflectionWithFullCopy(父类父类)
{
var parentClassLocal=JsonConvert.DeserializeObject(JsonConvert.SerializeObject(parentClass));
foreach(ParentProperties中的var p)
p、 调用(这是新的[]{p.GetMethod.Invoke(parentClassLocal,null)});
}
//只做一次,以获得最佳性能
私有静态属性info[]ParentProperties{get;}=typeof(ParentClass).GetProperties()。其中(c=>c.CanRead&&c.CanWrite).ToArray();
}
公共类父类
{
公共字符串属性1{get;set;}
公共int属性2{get;set;}
公共SomeClassWithObjectProperty对象属性{get;set;}
}
公共类SomeClassWithObjectProperty
{
公开一些