将类的属性从一个类复制到一个新的c#子类

本文关键字:一个 子类 复制 属性 | 更新日期: 2023-09-27 18:12:31

我有一个基类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ClassPropertyTest
{
    public class BaseClass
    {
        public string name { get; set; }
        public double value { get; set; }
    }
}

然后我有一些从基类

派生的类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ClassPropertyTest
{
    public class ClassA : BaseClass
    {
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ClassPropertyTest
{
    public class ClassB : BaseClass
    {
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ClassPropertyTest
{
    public class ClassC : BaseClass
    {
    }
}

我也有一个masterclass,每个类的实例都有属性。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ClassPropertyTest
{
    public class MasterClass
    {
        public ClassA ClassA { get; set; }
        public ClassB ClassB { get; set; }
        public ClassC ClassC { get; set; }
    }
}

现在,当我收到一个masterclass的实例时,我需要从属性ClassA创建一个新类。我使用反射,但我总是得到目标无效的错误。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
namespace ClassPropertyTest
{
    class Program
    {
        static void Main(string[] args)
        {
            //Fill master class
            MasterClass o_masterclass = new MasterClass();
            o_masterclass.ClassA = new ClassA { name = "classA", value = 1 };
            o_masterclass.ClassB = new ClassB { name = "classB", value = 2 };
            o_masterclass.ClassC = new ClassC { name = "classc", value = 3 };

            //Now get class by property name
            foreach (PropertyInfo prop in o_masterclass.GetType().GetProperties())
            {
                //Check for class A property
                if (prop.Name == "ClassA") {
                    //Create an instance of that type from ClassA
                    BaseClass instance = (BaseClass)Activator.CreateInstance(prop.PropertyType.BaseType);
                    //Copy properties from masterclass property ClassA to new instance of BaseClass
                    CopyObject<BaseClass>(prop.PropertyType.BaseType, ref instance);
                }
            }
            Console.ReadKey();
        }
        /// <summary>
        /// Copy an object to destination object, only matching fields will be copied
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sourceObject">An object with matching fields of the destination object</param>
        /// <param name="destObject">Destination object, must already be created</param>
        public static void CopyObject<T>(Type sourceObject, ref T destObject)
        {
            //  If either the source, or destination is null, return
            if (sourceObject == null || destObject == null)
                return;
            //  Get the type of each object
            Type sourceType = sourceObject;//.GetType();
            Type targetType = destObject.GetType();
            //  Loop through the source properties
            foreach (PropertyInfo p in sourceType.GetProperties())
            {
                //  Get the matching property in the destination object
                PropertyInfo targetObj = targetType.GetProperty(p.Name);
                //  If there is none, skip
                if (targetObj == null)
                    continue;
                //  Set the value in the destination
                targetObj.SetValue(destObject, p.GetValue(sourceObject, null), null);
            }
        }
    }
}
谁能帮我一下吗?谢谢。

@Sergey,我已经将CopyObject方法更改为:

  public static void CopyObject<T>(T sourceObject, ref T destObject)
        {
            //  If either the source, or destination is null, return
            if (sourceObject == null || destObject == null)
                return;
            //  Get the type of each object
            Type sourceType = sourceObject.GetType();
            Type targetType = destObject.GetType();
            //  Loop through the source properties
            foreach (PropertyInfo p in sourceType.GetProperties())
            {
                //  Get the matching property in the destination object
                PropertyInfo targetObj = targetType.GetProperty(p.Name);
                //  If there is none, skip
                if (targetObj == null)
                    continue;
                //  Set the value in the destination
                targetObj.SetValue(destObject, p.GetValue(sourceObject, null), null);
            }
        }

当我像你告诉我的那样调用它时,它有效。

 CopyObject<BaseClass>(o_masterclass.ClassA, ref instance);

但是在我的情况下o_masterclass。ClassA实际上是prop。propertytype。basetype。如何将此类型转换为所需的对象?

将类的属性从一个类复制到一个新的c#子类

您可以使用AutoMapper(或其他映射库):

var otherA = Mapper.Map<ClassA>(o_masterclass.ClassA);

默认情况下,它按名称映射属性。因此,在这种情况下,您甚至不需要设置任何配置。

在你的解决方案中,你应该传递sourceObject而不是它的type:

public static void CopyObject<T>(T sourceObject, ref T destObject)
{            
    if (sourceObject == null || destObject == null)
        return;
    //  Get the type of each object
    Type sourceType = sourceObject.GetType();
    Type targetType = destObject.GetType();
    //  Loop through the source properties
    foreach (PropertyInfo sourceProp in sourceType.GetProperties())
    {
        //  Get the matching property in the destination object
        PropertyInfo destProp = targetType.GetProperty(sourceProp.Name);
        //  If there is none, skip
        if (destProp == null)
            continue;
        //  Set the value in the destination
        object value = sourceProp.GetValue(sourceObject, null);
        destProp.SetValue(destObject, value, null);
    }
}

这样命名:

CopyObject<BaseClass>(o_masterclass.ClassA, ref instance);

还要记住,type可以包含索引器或只读属性。