使用C#将属性值复制到另一个对象

要将属性值从一个对象复制到另一个对象,我们通常使用以下语法实现:

ca.pro1 = cb.pro2; ca.pro2 = cb.pro2; 

其中ca和cb属于同一类。

有没有更简单的synatx或实用方法来帮助我们达到同样的效果?

谢谢。

 public static void CopyPropertiesTo(this T source, TU dest) { var sourceProps = typeof (T).GetProperties().Where(x => x.CanRead).ToList(); var destProps = typeof(TU).GetProperties() .Where(x => x.CanWrite) .ToList(); foreach (var sourceProp in sourceProps) { if (destProps.Any(x => x.Name == sourceProp.Name)) { var p = destProps.First(x => x.Name == sourceProp.Name); if(p.CanWrite) { // check if the property can be set or no. p.SetValue(dest, sourceProp.GetValue(source, null), null); } } } } 

这是我用于在ASP.NET MVC中的模型之间复制成员的函数。 当您寻找适用于相同类型的代码时,此代码也将支持具有相同属性的其他类型。

它使用reflection,但以更干净的方式。 注意Convert.ChangeType :你可能不需要它; 你可以检查类型而不是转换。

 public static TConvert ConvertTo(this object entity) where TConvert : new() { var convertProperties = TypeDescriptor.GetProperties(typeof(TConvert)).Cast(); var entityProperties = TypeDescriptor.GetProperties(entity).Cast(); var convert = new TConvert(); foreach (var entityProperty in entityProperties) { var property = entityProperty; var convertProperty = convertProperties.FirstOrDefault(prop => prop.Name == property.Name); if (convertProperty != null) { convertProperty.SetValue(convert, Convert.ChangeType(entityProperty.GetValue(entity), convertProperty.PropertyType)); } } return convert; } 

由于这是一种扩展方法,因此用法很简单:

 var result = original.ConvertTo(); 

如果我没有弄错所需,那么在两个现有实例 (即使不是同一类型) 之间轻松实现属性值复制的方法是使用Automapper 。

  1. 创建映射配置
  2. 然后调用.Map(soure,target)

只要你保持属性在相同的类型和相同的命名约定中,所有都应该工作。

例:

 MapperConfiguration _configuration = new MapperConfiguration(cnf => { cnf.CreateMap(); }); var mapper = new Mapper(_configuration); maper.DefaultContext.Mapper.Map(source, target) 

并不是的。 有一个MemberwiseClone(),但直接复制引用意味着你将获得对同一个对象的引用,这可能是坏的。 您可以实现ICloneable接口并将其用于深层复制。 我更喜欢制作自己的Clone()方法,因为ICloneable接口返回需要强制转换的Object。

  public static TTarget Convert(TSource sourceItem) { if (null == sourceItem) { return default(TTarget); } var deserializeSettings = new JsonSerializerSettings { ObjectCreationHandling = ObjectCreationHandling.Replace, }; var serializedObject = JsonConvert.SerializeObject(sourceItem, deserializeSettings); return JsonConvert.DeserializeObject(serializedObject); } 

用法:

  promosion = YourClass.Convert(existsPromosion);