C#反思 - 合并两个对象组合在一起 [英] C# Reflection - merge two objects together

查看:135
本文介绍了C#反思 - 合并两个对象组合在一起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有需要,如果空更新对象的财产与从对象B的等价的属性,如果不为null。我希望我的代码可以使用各种对象。



我有一个版本的工作,直到其中一个对象包含的类型列表中,这是我有一个空白在下面的代码的PROPERT。我的主要问题是如何能够最好地实现这部分代码。其次是有在做这件事的一个更好的方法;第三,我知道它永远不会是迅速的,但任何建议加快步伐,将不胜感激。



在此先感谢

 公共牛逼MergeWith< T,U>(T primarySource,U secondarySource),其中U:类,T 
{
型primaryType = typeof运算(T);
型secondaryType = typeof运算(U);
的foreach(的PropertyInfo primaryInfo在primaryType.GetProperties())
{
如果(primaryInfo.CanWrite)
{
对象currentPrimary = primaryInfo.GetValue(primarySource,NULL) ;

的PropertyInfo secondaryInfo = secondaryType.GetProperty(primaryInfo.Name);
对象currentSecondary = secondaryInfo.GetValue(secondarySource,NULL);

如果(currentPrimary == NULL和放大器;&安培;!currentSecondary = NULL)
{
primaryInfo.SetValue(primarySource,currentSecondary,NULL);
}
,否则如果((currentPrimary = NULL&放大器;!&安培; currentSecondary = NULL)及!&安培; isChildClass(primaryInfo))
{
如果(isCollection(currentPrimary) )
{
//这里


}
,否则
{
MethodInfo的方法= typeof运算(NavigationModel).GetMethod( 合并);
MethodInfo的通用= method.MakeGenericMethod(primaryInfo.PropertyType,primaryInfo.PropertyType);
对象returnChild = generic.Invoke(这一点,新的对象[2] {currentPrimary,currentSecondary});
}
}
}
}

返回primarySource;
}

私人布尔isCollection(对象属性)
{
返回的typeof(ICollection的).IsAssignableFrom(property.GetType())
||的typeof(ICollection的&所述;>)。IsAssignableFrom(property.GetType());
}


私人布尔isChildClass(的PropertyInfo的PropertyInfo)
{
回报率(propertyInfo.PropertyType.IsClass&安培;&安培;!propertyInfo.PropertyType.IsValueType &功放;&安培;
propertyInfo.PropertyType.IsPrimitive&放大器;!&安培;!propertyInfo.PropertyType.FullName =System.String);
}


解决方案

我已经创建了下面的扩展在我的最新项目的使用方法,并能正常工作,集合所有。这是一个非常的你在你的方法在做什么简单的版本。与矿井两个类必须是相同的类型。 ?你跟集合遇到什么问题。



 公共静态类ExtensionMethods 
{
公共静态TEntity CopyTo从< TEntity>(这TEntity OriginalEntity,TEntity NewEntity)
{
的PropertyInfo [] = oProperties OriginalEntity.GetType()的GetProperties()。

的foreach(的PropertyInfo CurrentProperty在oProperties.Where(P => p.CanWrite))
{
如果(CurrentProperty.GetValue(NewEntity,NULL)!= NULL)
{
CurrentProperty.SetValue(OriginalEntity,CurrentProperty.GetValue(NewEntity,NULL),NULL);
}
}

返回OriginalEntity;
}
}


I have a need to update object A's property if null with that from object B's equivalent property if that is not null. I wanted code I can use for various objects.

I had a version working until one of the objects contained a propert of type List, which is where I have a blank in the code below. My main question is how can I best implement this part of the code. Secondly is there a better way of doing this whole thing and thirdly I know its never going to be rapid but any suggestions to speed it up would be appreciated.

Thanks in advance.

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

解决方案

I have created the below extension method for use in my latest project and it works fine, collections and all. It is a pretty much a simpler version of what you are doing in your method. With mine both classes have to be the same type. What problem do you encounter with collections?

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

这篇关于C#反思 - 合并两个对象组合在一起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆