C# 动态合并2个类
我有两个类实现相同的接口。这两个类中的一个类比另一个类具有更多的属性。现在,我正在寻找一种解决方案,以在较大的类中使用较少的属性来填充该类 实际上,我可能会使用手动映射器。我知道,但这必须适用于调用此函数的每个类。我已经读到一个C# 动态合并2个类,c#,.net,generics,lambda,func,C#,.net,Generics,Lambda,Func,我有两个类实现相同的接口。这两个类中的一个类比另一个类具有更多的属性。现在,我正在寻找一种解决方案,以在较大的类中使用较少的属性来填充该类 实际上,我可能会使用手动映射器。我知道,但这必须适用于调用此函数的每个类。我已经读到一个lambda表达式可以作为我的解决方案,但我不知道如何为每个接口/类编写通用的和动态的表达式 我的问题是不创建新实例,而是使用现有实例。有人有想法吗?如果你不介意使用第三方工具,那么这件事做得相当好 来自AutoMapper文档的示例: Mapper.CreateMap&
lambda表达式可以作为我的解决方案,但我不知道如何为每个接口/类编写通用的和动态的表达式
我的问题是不创建新实例,而是使用现有实例。有人有想法吗?如果你不介意使用第三方工具,那么这件事做得相当好
来自AutoMapper文档的示例:
Mapper.CreateMap<Order, OrderDto>();
OrderDto dto = Mapper.Map<OrderDto>(order);
Mapper.CreateMap();
OrderDto-dto=Mapper.Map(order);
如果您不介意使用第三方工具,那么这件事做得相当好
来自AutoMapper文档的示例:
Mapper.CreateMap<Order, OrderDto>();
OrderDto dto = Mapper.Map<OrderDto>(order);
Mapper.CreateMap();
OrderDto-dto=Mapper.Map(order);
如果您不介意使用第三方工具,您可以使用此代码段并根据需要进行调整:
public B Convert<A, B>(A element) where B : A, new()
{
//get the interface's properties that implement both a getter and a setter
IEnumerable<PropertyInfo> properties = typeof(A)
.GetProperties()
.Where(property => property.CanRead && property.CanWrite).ToList();
//create new object
B b = new B();
//copy the property values to the new object
foreach (var property in properties)
{
//read value
object value = property.GetValue(element);
//set value
property.SetValue(b, value);
}
return b;
}
public B Convert(A元素),其中B:A,new()
{
//获取实现getter和setter的接口属性
IEnumerable properties=typeof(A)
.GetProperties()
.Where(property=>property.CanRead&&property.CanWrite).ToList();
//创建新对象
B=新的B();
//将特性值复制到新对象
foreach(属性中的var属性)
{
//读取值
对象值=property.GetValue(元素);
//设定值
属性。设置值(b,值);
}
返回b;
}
如果您不介意使用第三方工具,您可以使用此代码段并根据需要进行调整:
public B Convert<A, B>(A element) where B : A, new()
{
//get the interface's properties that implement both a getter and a setter
IEnumerable<PropertyInfo> properties = typeof(A)
.GetProperties()
.Where(property => property.CanRead && property.CanWrite).ToList();
//create new object
B b = new B();
//copy the property values to the new object
foreach (var property in properties)
{
//read value
object value = property.GetValue(element);
//set value
property.SetValue(b, value);
}
return b;
}
public B Convert(A元素),其中B:A,new()
{
//获取实现getter和setter的接口属性
IEnumerable properties=typeof(A)
.GetProperties()
.Where(property=>property.CanRead&&property.CanWrite).ToList();
//创建新对象
B=新的B();
//将特性值复制到新对象
foreach(属性中的var属性)
{
//读取值
对象值=property.GetValue(元素);
//设定值
属性。设置值(b,值);
}
返回b;
}
公共静态void BusinessObjectConverter(此t目标,TSource源)
{
//Lesen aller酒店
var targetProperties=typeof(TTarget)
.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)
.Where(prop=>prop.CanWrite);
//Lesen aller酒店
var sourceProperties=typeof(TTarget)
.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)
.Where(prop=>prop.CanWrite);
//回路杜尔恰尔特性
foreach(targetProperties.Intersect(sourceProperties)中的var项)
{
//里尔·沃特·杜尔芬·尼希特·格什里本·沃登
如果(!string.IsNullOrEmpty(source.GetType().GetProperty(item.Name).GetValue(source,null.ToString()))
{
//Befuellen der Properties im Zielobjekt
item.SetValue(目标,ValueConverter.ConvertValue(
source.GetType().GetProperty(item.Name).GetValue(source,null),
项目。属性类型),
无效);
}
}
}
公共静态void BusinessObjectConverter(此t目标,TSource源)
{
//Lesen aller酒店
var targetProperties=typeof(TTarget)
.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)
.Where(prop=>prop.CanWrite);
//Lesen aller酒店
var sourceProperties=typeof(TTarget)
.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)
.Where(prop=>prop.CanWrite);
//回路杜尔恰尔特性
foreach(targetProperties.Intersect(sourceProperties)中的var项)
{
//里尔·沃特·杜尔芬·尼希特·格什里本·沃登
如果(!string.IsNullOrEmpty(source.GetType().GetProperty(item.Name).GetValue(source,null.ToString()))
{
//Befuellen der Properties im Zielobjekt
item.SetValue(目标,ValueConverter.ConvertValue(
source.GetType().GetProperty(item.Name).GetValue(source,null),
项目。属性类型),
无效);
}
}
}
由于代表率低,我无法添加评论,因此必须发布答案
根据Automapper的回答,手动创建映射可能是一件痛苦的事情。要动态执行此操作,我使用:
TDestination destination = new TDestination();
AutoMapper.Mapper.Map<TSource, TDestination>(source, destination, opts => opts.CreateMissingTypeMaps = true);
TDestination destination=新的TDestination();
AutoMapper.Mapper.Map(源、目标、opts=>opts.CreateMissingTypeMaps=true);
您需要记住的是确保属性名称匹配由于代表性低,我无法添加评论,因此必须发布答案
根据Automapper的回答,手动创建映射可能是一件痛苦的事情。要动态执行此操作,我使用:
TDestination destination = new TDestination();
AutoMapper.Mapper.Map<TSource, TDestination>(source, destination, opts => opts.CreateMissingTypeMaps = true);
TDestination destination=新的TDestination();
AutoMapper.Mapper.Map(源、目标、opts=>opts.CreateMissingTypeMaps=true);
您需要记住的就是确保属性名称匹配使用反射来遍历“small”类型的属性。对于每个属性,在“大”类型上查找具有相同名称的属性。从大型类型的实例读取,然后写入小型类型的新实例。使用反射遍历“小型”类型的属性。对于每个属性,查找具有相同名称的属性