Entity framework 是否有较短的方法将原始实体对象的属性覆盖到当前实体对象?
例如,我有:Entity framework 是否有较短的方法将原始实体对象的属性覆盖到当前实体对象?,entity-framework,c#-4.0,Entity Framework,C# 4.0,例如,我有: originalItem.Property1 = currentItem.Property1; originalItem.Property2 = currentItem.Property2; originalItem.Property3 = currentItem.Property3; originalItem.Property4 = currentItem.Property4; 如果currentItem的属性值与originalItem的属性值不同,则属性也将更改 这里有捷径
originalItem.Property1 = currentItem.Property1;
originalItem.Property2 = currentItem.Property2;
originalItem.Property3 = currentItem.Property3;
originalItem.Property4 = currentItem.Property4;
如果currentItem的属性值与originalItem的属性值不同,则属性也将更改
这里有捷径吗?谢谢。是的,您可以通过
读取两个实例的属性,并在反射的帮助下分配它们
这里有一些代码
public static void AssignSourceToDestination(Object source, ref Object destination)
{
IList<PropertyInfo> sourceProps = source.GetProperties();
IList<PropertyInfo> destProps = destination.GetProperties();
foreach (PropertyInfo property in destProps)
{
if (property.CanWrite)
{
PropertyInfo sourceProp = sourceProps.Where(p => p.Name.Equals(property.Name) &&
p.PropertyType.Equals(property.PropertyType) && p.CanRead).First();
if (null != sourceProp)
property.SetValue(destination, sourceProp.GetValue(source, null), null);
}
}
}
public static IList<PropertyInfo> GetProperties(this Object me)
{
return me.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance).ToList();
}
公共静态void AssignSourceToDestination(对象源,引用对象目标)
{
IList sourceProps=source.GetProperties();
IList destProps=destination.GetProperties();
foreach(destProps中的PropertyInfo属性)
{
if(property.CanWrite)
{
PropertyInfo sourceProp=sourceProps.Where(p=>p.Name.Equals(property.Name)&&
p、 PropertyType.Equals(property.PropertyType)和&p.CanRead.First();
if(null!=sourceProp)
SetValue(目标,sourceProp.GetValue(源,null),null);
}
}
}
公共静态IList GetProperties(此对象为me)
{
return me.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance).ToList();
}
是的,您可以通过
读取两个实例的属性,并在反射的帮助下分配它们
这里有一些代码
public static void AssignSourceToDestination(Object source, ref Object destination)
{
IList<PropertyInfo> sourceProps = source.GetProperties();
IList<PropertyInfo> destProps = destination.GetProperties();
foreach (PropertyInfo property in destProps)
{
if (property.CanWrite)
{
PropertyInfo sourceProp = sourceProps.Where(p => p.Name.Equals(property.Name) &&
p.PropertyType.Equals(property.PropertyType) && p.CanRead).First();
if (null != sourceProp)
property.SetValue(destination, sourceProp.GetValue(source, null), null);
}
}
}
public static IList<PropertyInfo> GetProperties(this Object me)
{
return me.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance).ToList();
}
公共静态void AssignSourceToDestination(对象源,引用对象目标)
{
IList sourceProps=source.GetProperties();
IList destProps=destination.GetProperties();
foreach(destProps中的PropertyInfo属性)
{
if(property.CanWrite)
{
PropertyInfo sourceProp=sourceProps.Where(p=>p.Name.Equals(property.Name)&&
p、 PropertyType.Equals(property.PropertyType)和&p.CanRead.First();
if(null!=sourceProp)
SetValue(目标,sourceProp.GetValue(源,null),null);
}
}
}
公共静态IList GetProperties(此对象为me)
{
return me.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance).ToList();
}