如何深度复制强类型集合的成员

h4ck3r8ug5

我有两个XmlPerson和Person类,每个类都有公共属性,没有方法也没有任何字段。

如何将所有属性从Person深复制到XmlPerson?我不想使用像MiscUtil.PropertyCopy或Automapper这样的第三方库。我已经设法复制了原始类型和强类型对象的“第一层”属性,但是当列表出现时我不知道。

Person类的结构如下:

public class Person
{
    public string FirstName { get; set; }

    public string Surname { get; set; }

    public decimal? Salary { get; set; }

    public List<AddressDetails> AddressDetails { get; set; }

    public NextOfKin NextOfKin { get; set; }
}

public class NextOfKin
{
    public string FirstName { get; set; }

    public string Surname { get; set; }

    public string ContactNumber { get; set; }

    public List<AddressDetails> AddressDetails { get; set; }
}

public class AddressDetails
{
    public int HouseNumber { get; set; }

    public string StreetName { get; set; }

    public string City { get; set; }
}

谢谢你的帮助。查尔斯

这是我到目前为止所拥有的:

公共类XmlTestCaseToClassMapper {内部TTarget MapXmlClassTotargetClass(TSource xmlPerson){var targetObject = Activator.CreateInstance(); var sourceObject = Activator.CreateInstance();

        //var xmlClassProperties = xmlPerson.GetType().GetProperties().ToList().OrderBy(x => x.Name);

        var xmlClassProperties = GetProperties(xmlPerson.GetType());

        //var targetClassProperties = targetObject.GetType().GetProperties().ToList().OrderBy(x => x.Name);

        var targetClassProperties = GetProperties(targetObject.GetType());

        PropertyInfo targetClassProperty = null;

        foreach (var xmlProperty in xmlClassProperties)
        {
            if (!xmlProperty.PropertyType.IsClass || xmlProperty.PropertyType.UnderlyingSystemType == typeof(string)
                || xmlProperty.PropertyType.IsPrimitive)
            {
                targetClassProperty = targetClassProperties.ToList().FirstOrDefault(x => x.Name == xmlProperty.Name);

                var propertyValue = xmlProperty.GetValue(xmlPerson, null);

                targetClassProperty.SetValue(targetObject, propertyValue, null);
            }

            else if (xmlProperty.PropertyType.UnderlyingSystemType == typeof(NextOfKin)) //Check subType of the property
            {
                var subPropertyInstance = Activator.CreateInstance(xmlProperty.GetType());
                var subProperties = GetProperties(xmlProperty.GetType());

                subProperties.ForEach(subProperty =>
                {
                    targetClassProperty = targetClassProperties.ToList().FirstOrDefault(x => x.Name == subProperty.Name && x.GetType().IsClass);
                    targetClassProperty.SetValue(subPropertyInstance, xmlProperty.GetValue(this, null), null);
                });
            }

            //else if (xmlProperty.PropertyType.IsGenericType)

            //{

            //        var xmlGenericType = xmlProperty.PropertyType.GetGenericArguments().First();

            //        var xmlGenericTypeProperties = GetProperties(xmlGenericType);

            //        targetClassProperty = targetClassProperties.ToList().FirstOrDefault(x => x.Name == xmlProperty.Name);

            //        var targetGenericType = targetClassProperty.PropertyType.GetGenericArguments().First();

            //        var targetGenericProperties = GetProperties(targetGenericType);

            //        Type targetGenericList = typeof(List<>).MakeGenericType(new Type[] { targetGenericType });

            //        object listInstance = Activator.CreateInstance(targetGenericList);

            //    //foreach (var xmlGenericProperty in xmlGenericTypeProperties)

            //    //{

            //    //    var targetGenericProperty = targetGenericProperties.FirstOrDefault(x => x.Name == xmlGenericProperty.Name);

            //    //    targetGenericProperty.SetValue(targetGenericProperty, xmlGenericProperty.GetValue(xmlGenericType, null), null);

            //    //}

            //    xmlGenericTypeProperties.ForEach(x =>

            //    {

            //        foreach (var targetGenericProperty in targetGenericProperties)

            //        {

            //            targetGenericProperty.SetValue(targetGenericProperty, targetGenericProperty.GetValue(x, null), null);

            //        }

            //    });

            //}

            //}

        }
        return targetObject;
    }

    private List<PropertyInfo> GetProperties(Type targetType)
    {
        var properties = new List<PropertyInfo>();

        targetType.GetProperties(BindingFlags.Instance | BindingFlags.Public).ToList().ForEach(property =>
        {

            properties.Add(property);

        });

        return properties.OrderBy(x => x.Name).ToList();
    }
}
瓦格纳·多斯·安霍斯(Wagner DosAnjos)

这是一个可能的解决方案。它可能需要根据您的实际类进行一些调整。

public T DeepCopy<S, T>(S source) where T : new()
{
    var sourceProperties = typeof(S).GetProperties(BindingFlags.Instance | BindingFlags.Public);
    T target = new T();

    foreach (var sourceProperty in sourceProperties)
    {
        var property = typeof(T).GetProperty(sourceProperty.Name);

        if (property.PropertyType.IsPrimitive || 
            property.PropertyType == typeof(string) || 
            (property.PropertyType.IsGenericType && property.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>)))
        {
            object value = sourceProperty.GetValue(source);

            property.SetValue(target, value);
        }
        else if (property.PropertyType.IsGenericType && property.PropertyType.GetGenericTypeDefinition() == typeof(List<>))
        {
            var sourceList = (IEnumerable)sourceProperty.GetValue(source);

            if (sourceList != null)
            {
                var deepCopy = this.GetType().GetMethod("DeepCopy").MakeGenericMethod(sourceProperty.PropertyType.GenericTypeArguments[0], property.PropertyType.GenericTypeArguments[0]);
                var ctor = property.PropertyType.GetConstructor(Type.EmptyTypes);

                IList targetList = (IList) ctor.Invoke(null);

                foreach (var element in sourceList)
                {
                    targetList.Add(deepCopy.Invoke(this, new object[] { element } ));
                }

                property.SetValue(target, targetList);
            }
        }
        else
        {
            var value = sourceProperty.GetValue(source);

            if (value != null)
            {
                var deepCopy = this.GetType().GetMethod("DeepCopy").MakeGenericMethod(sourceProperty.PropertyType, property.PropertyType);

                property.SetValue(target, deepCopy.Invoke(this, new object[] { value }));
            }
        }
    }

    return target;
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

F#:如何用强类型表示有限的集合?

来自分类Dev

在成员函数的默认参数中使用强类型枚举的成员

来自分类Dev

如何制作强类型的AuthorizeAttribute

来自分类Dev

如何使用强类型枚举

来自分类Dev

如何在Javascript中使用数组成员深度复制(克隆)对象?

来自分类Dev

如何获取成员扩展或实现另一个成员的传递集合<泛型类型>的子集合?

来自分类Dev

自动装配强类型集合时出现问题

来自分类Dev

自动装配强类型集合时出现问题

来自分类Dev

如何对LpVariables进行深度复制

来自分类Dev

遍历集合的成员,如何获取成员的密钥?

来自分类Dev

遍历集合的成员,如何获取成员的密钥?

来自分类Dev

如何删除集合类型?

来自分类Dev

如何从LINQ查询将匿名类型转换为强类型

来自分类Dev

如何获得强类型的给定类型的属性名称?

来自分类Dev

如何进行适当的深度复制?

来自分类Dev

如何比较和深度复制反应

来自分类Dev

如何获得集合成员所属的模型

来自分类Dev

如何使用C#从xml获取强类型对象?

来自分类Dev

设计时如何进行强类型数据绑定?

来自分类Dev

如何获得强类型的DropDownList绑定到控件Action

来自分类Dev

如何将对象转换为强类型的XML

来自分类Dev

我如何要求枚举作为强类型参数?

来自分类Dev

如何根据下拉选择填充强类型文本框

来自分类Dev

如何在C ++ 11中定义强ID类型?

来自分类Dev

如何构建具有属性的强类型XML POST请求

来自分类Dev

如何从MVC中的视图模型创建强类型的局部视图?

来自分类Dev

如何从强类型模型填充动态添加的下拉列表

来自分类Dev

如何为枚举下拉列表创建强类型标签

来自分类Dev

如何在Rails4中分配深度嵌套的服装的强参数?

Related 相关文章

  1. 1

    F#:如何用强类型表示有限的集合?

  2. 2

    在成员函数的默认参数中使用强类型枚举的成员

  3. 3

    如何制作强类型的AuthorizeAttribute

  4. 4

    如何使用强类型枚举

  5. 5

    如何在Javascript中使用数组成员深度复制(克隆)对象?

  6. 6

    如何获取成员扩展或实现另一个成员的传递集合<泛型类型>的子集合?

  7. 7

    自动装配强类型集合时出现问题

  8. 8

    自动装配强类型集合时出现问题

  9. 9

    如何对LpVariables进行深度复制

  10. 10

    遍历集合的成员,如何获取成员的密钥?

  11. 11

    遍历集合的成员,如何获取成员的密钥?

  12. 12

    如何删除集合类型?

  13. 13

    如何从LINQ查询将匿名类型转换为强类型

  14. 14

    如何获得强类型的给定类型的属性名称?

  15. 15

    如何进行适当的深度复制?

  16. 16

    如何比较和深度复制反应

  17. 17

    如何获得集合成员所属的模型

  18. 18

    如何使用C#从xml获取强类型对象?

  19. 19

    设计时如何进行强类型数据绑定?

  20. 20

    如何获得强类型的DropDownList绑定到控件Action

  21. 21

    如何将对象转换为强类型的XML

  22. 22

    我如何要求枚举作为强类型参数?

  23. 23

    如何根据下拉选择填充强类型文本框

  24. 24

    如何在C ++ 11中定义强ID类型?

  25. 25

    如何构建具有属性的强类型XML POST请求

  26. 26

    如何从MVC中的视图模型创建强类型的局部视图?

  27. 27

    如何从强类型模型填充动态添加的下拉列表

  28. 28

    如何为枚举下拉列表创建强类型标签

  29. 29

    如何在Rails4中分配深度嵌套的服装的强参数?

热门标签

归档