C#Reflection - 将两个对象合并在一起

tjh*_*jh7 2 c#

我需要更新对象A的属性,如果null为对象B的等效属性,如果它不为null.我想要可以用于各种对象的代码.

我有一个版本工作,直到其中一个对象包含List类型的属性,这是我在下面的代码中有空白的地方.我的主要问题是如何才能最好地实现这部分代码.其次是有更好的方法来做这件事,第三,我知道它永远不会很快,但任何加快它的建议都会受到赞赏.

提前致谢.

public T MergeWith<T, U>(T primarySource, U secondarySource) where U : class, T
    {
        Type primaryType = typeof(T);
        Type secondaryType = typeof(U);
        foreach (PropertyInfo primaryInfo in primaryType.GetProperties())
        {
            if (primaryInfo.CanWrite)
            {
                object currentPrimary = primaryInfo.GetValue(primarySource, null);

                PropertyInfo secondaryInfo = secondaryType.GetProperty(primaryInfo.Name);
                object currentSecondary = secondaryInfo.GetValue(secondarySource, null);

                if (currentPrimary == null && currentSecondary != null)
                {
                    primaryInfo.SetValue(primarySource, currentSecondary, null);
                }
                else if ((currentPrimary != null && currentSecondary != null) && isChildClass(primaryInfo))
                {
                    if (isCollection(currentPrimary))
                    {
                        // here


                    }
                    else
                    {
                        MethodInfo method = typeof(NavigationModel).GetMethod("MergeWith");
                        MethodInfo generic = method.MakeGenericMethod(primaryInfo.PropertyType, primaryInfo.PropertyType);
                        object returnChild = generic.Invoke(this, new object[2] { currentPrimary, currentSecondary });
                    }
                }
            }
        }

        return primarySource;
    }

    private bool isCollection(object property) 
    {
        return typeof(ICollection).IsAssignableFrom(property.GetType())
            || typeof(ICollection<>).IsAssignableFrom(property.GetType()); 
    }


    private bool isChildClass(PropertyInfo propertyInfo)
    {
        return (propertyInfo.PropertyType.IsClass && !propertyInfo.PropertyType.IsValueType &&
                            !propertyInfo.PropertyType.IsPrimitive && propertyInfo.PropertyType.FullName != "System.String");
    }
Run Code Online (Sandbox Code Playgroud)

Ben*_*son 7

我创建了以下扩展方法,以便在我的最新项目中使用,它工作正常,集合和所有.这是你在方法中所做的几乎简单的版本.我的两个班级必须是同一类型.你在收藏中遇到什么问题?

    public static class ExtensionMethods
    {
        public static TEntity CopyTo<TEntity>(this TEntity OriginalEntity, TEntity NewEntity)
        {
            PropertyInfo[] oProperties = OriginalEntity.GetType().GetProperties();

            foreach (PropertyInfo CurrentProperty in oProperties.Where(p => p.CanWrite))
            {
                if (CurrentProperty.GetValue(NewEntity, null) != null)
                {
                    CurrentProperty.SetValue(OriginalEntity, CurrentProperty.GetValue(NewEntity, null), null);
                }
            }

            return OriginalEntity;
        }
    }
Run Code Online (Sandbox Code Playgroud)