我想在C#中使用反射将实体对象Map到业务对象-
public class Category { public int CategoryID { get; set; } public string CategoryName { get; set; } }
public class Category
{
public int CategoryID { get; set; }
public string CategoryName { get; set; }
}
我的实体类具有相同的属性,CategoryId和CategoryName。任何使用反射或动态的最佳实践都将受到欢迎。
z4bn682m1#
http://automapper.codeplex.com/我的票是自动绘图仪。我目前使用这个。我已经对它做了性能测试,虽然它没有手工Map那么快(下图):
Category c = new Category { id = entity.id, name = entity.name };
Category c = new Category
id = entity.id,
name = entity.name
};
自动Map的折衷使其成为一个明显的选择,至少对于实体到业务层的Map来说是这样。实际上,我手动将业务层Map到视图模型,因为我需要灵活性。
nhhxz33t2#
这是我写的东西,我认为你正在尝试做的事情,你不需要强制转换你的业务对象:
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 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()); }
public static Category GetCategory(int id)
return ConvertObjectToEntity(Database.GetCategoryEntity(id), new Category());
mw3dktmi3#
如果你需要高性能的解决方案,反射可能不是正确的选择。除了其他答案,我可以建议使用预编译的表达式,因为它们更有效,并且通过使用它们,您可以从类型安全中受益。你可以在Medium上的article中阅读更多关于这个想法的信息。您还可以查看TryAtSoftware.Extensions.Reflection-它公开了可以帮助构造适当表达式的扩展方法。它是open-source,可以作为NuGet包使用。
TryAtSoftware.Extensions.Reflection
62o28rlo4#
可以使用Automapper或Valueinjecter编辑:好的,我写了一个使用反射的函数,注意它不能处理Map的属性不完全相等的情况,例如IList不能Map到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 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);
4条答案
按热度按时间z4bn682m1#
http://automapper.codeplex.com/
我的票是自动绘图仪。我目前使用这个。我已经对它做了性能测试,虽然它没有手工Map那么快(下图):
自动Map的折衷使其成为一个明显的选择,至少对于实体到业务层的Map来说是这样。实际上,我手动将业务层Map到视图模型,因为我需要灵活性。
nhhxz33t2#
这是我写的东西,我认为你正在尝试做的事情,你不需要强制转换你的业务对象:
用法示例:
mw3dktmi3#
如果你需要高性能的解决方案,反射可能不是正确的选择。除了其他答案,我可以建议使用预编译的表达式,因为它们更有效,并且通过使用它们,您可以从类型安全中受益。你可以在Medium上的article中阅读更多关于这个想法的信息。
您还可以查看
TryAtSoftware.Extensions.Reflection
-它公开了可以帮助构造适当表达式的扩展方法。它是open-source,可以作为NuGet包使用。62o28rlo4#
可以使用Automapper或Valueinjecter
编辑:
好的,我写了一个使用反射的函数,注意它不能处理Map的属性不完全相等的情况,例如IList不能Map到List