将类属性从类复制到新的子类C#

时间:2014-07-11 08:49:21

标签: c# reflection properties

我是基类

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。如何将此类型转换为所需对象?

1 个答案:

答案 0 :(得分:2)

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

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

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

在您的解决方案中,您应该传递sourceObject而不是它的类型:

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);

另请注意,类型可以包含索引器或只读属性。