.net 使用反射c映射业务对象和实体对象#

.net 使用反射c映射业务对象和实体对象#,.net,entity-framework,c#-4.0,c#-3.0,.net,Entity Framework,C# 4.0,C# 3.0,我想用c#中的反射将实体对象映射到业务对象 我的实体类具有相同的属性CategoryId和CategoryName。任何使用反射或动态的最佳实践都将不胜感激。您可以使用或 编辑: 好的,我写了一个使用反射的函数,注意它不会处理映射属性不完全相等的情况,例如IList不会映射到List public static void MapObjects(object source, object destination) { Type sourcetype = source.GetType();

我想用c#中的反射将实体对象映射到业务对象

我的实体类具有相同的属性CategoryId和CategoryName。任何使用反射或动态的最佳实践都将不胜感激。

您可以使用或

编辑:

好的,我写了一个使用反射的函数,注意它不会处理映射属性不完全相等的情况,例如IList不会映射到List

public static void MapObjects(object source, object destination)
{
    Type sourcetype = source.GetType();
    Type destinationtype = destination.GetType();

    var sourceProperties = sourcetype.GetProperties();
    var destionationProperties = destinationtype.GetProperties();

    var commonproperties = from sp in sourceProperties
                           join dp in destionationProperties on new {sp.Name, sp.PropertyType} equals
                               new {dp.Name, dp.PropertyType}
                           select new {sp, dp};

    foreach (var match in commonproperties)
    {
        match.dp.SetValue(destination, match.sp.GetValue(source, null), null);                   
    }            
}

我投票支持自动制图器。我现在用这个。我已经对它进行了性能测试,尽管它没有手动映射快(如下所示):


映射的折衷自动使其成为一个明显的选择,至少对于实体到业务层的映射来说是如此。实际上,我将地图从业务层手动映射到视图模型,因为我需要灵活性。

我写这篇文章是为了做我认为您正在尝试做的事情,您不必强制转换您的业务对象:

public static TEntity ConvertObjectToEntity<TEntity>(object objectToConvert, TEntity entity) where TEntity : class
    {
        if (objectToConvert == null || entity == null)
        {
            return null;
        }

        Type BusinessObjectType = entity.GetType();
        PropertyInfo[] BusinessPropList = BusinessObjectType.GetProperties();

        Type EntityObjectType = objectToConvert.GetType();
        PropertyInfo[] EntityPropList = EntityObjectType.GetProperties();

        foreach (PropertyInfo businessPropInfo in BusinessPropList)
        {
            foreach (PropertyInfo entityPropInfo in EntityPropList)
            {
                if (entityPropInfo.Name == businessPropInfo.Name && !entityPropInfo.GetGetMethod().IsVirtual && !businessPropInfo.GetGetMethod().IsVirtual)
                {
                    businessPropInfo.SetValue(entity, entityPropInfo.GetValue(objectToConvert, null), null);
                    break;
                }
            }
        }

        return entity;
    }

我不想使用任何第三方或打开sourcestring destinationPropertyName=LookupMapping(sourceType.ToString(),destinationType.ToString(),sourceProperty.Name);这里LookupMapping是Microsoft Dynamic CRM功能。如果没有CRMi,我怎么做?我需要使用反射来避免固定数量的属性。我想知道是什么让你认为它不使用反射?GetValue,SetValue这些都是反射方法。我最近开始处理一个新项目,并在其中找到了此代码的复制版本。我强烈建议使用@Serguzest最初建议的AutoMapper或ValueInjector,因为这段代码在我们的应用程序中产生了问题。例如,目标中的列表将为空。
Category c = new Category 
  {
    id = entity.id,
    name = entity.name
  };
public static TEntity ConvertObjectToEntity<TEntity>(object objectToConvert, TEntity entity) where TEntity : class
    {
        if (objectToConvert == null || entity == null)
        {
            return null;
        }

        Type BusinessObjectType = entity.GetType();
        PropertyInfo[] BusinessPropList = BusinessObjectType.GetProperties();

        Type EntityObjectType = objectToConvert.GetType();
        PropertyInfo[] EntityPropList = EntityObjectType.GetProperties();

        foreach (PropertyInfo businessPropInfo in BusinessPropList)
        {
            foreach (PropertyInfo entityPropInfo in EntityPropList)
            {
                if (entityPropInfo.Name == businessPropInfo.Name && !entityPropInfo.GetGetMethod().IsVirtual && !businessPropInfo.GetGetMethod().IsVirtual)
                {
                    businessPropInfo.SetValue(entity, entityPropInfo.GetValue(objectToConvert, null), null);
                    break;
                }
            }
        }

        return entity;
    }
public static Category GetCategory(int id)
    {
        return ConvertObjectToEntity(Database.GetCategoryEntity(id), new Category());
    }