C# 4.0 获取实体对象';s属性名称,不包括entitycollection和entityreference
我正在研究一种使用反射比较两个对象的方法。对象类型是从实体框架创建的对象。当我使用GetProperties()时,我得到的是EntityCollection和EntityReference属性。我只需要属于对象的属性,而不需要任何关联的属性或外键的引用 我试过以下方法 我考虑过传递一个属性数组进行比较,但我不想为每种对象类型都输入它们。我愿意接受一些建议,即使是那些不使用反思的建议C# 4.0 获取实体对象';s属性名称,不包括entitycollection和entityreference,c#-4.0,entity-framework-4,C# 4.0,Entity Framework 4,我正在研究一种使用反射比较两个对象的方法。对象类型是从实体框架创建的对象。当我使用GetProperties()时,我得到的是EntityCollection和EntityReference属性。我只需要属于对象的属性,而不需要任何关联的属性或外键的引用 我试过以下方法 我考虑过传递一个属性数组进行比较,但我不想为每种对象类型都输入它们。我愿意接受一些建议,即使是那些不使用反思的建议 public bool CompareEntities<T>(T oldEntity, T newE
public bool CompareEntities<T>(T oldEntity, T newEntity)
{
bool same = true;
PropertyInfo[] properties = oldEntity.GetType().GetProperties();
foreach (PropertyInfo property in properties)
{
var oldValue = property.GetValue(oldEntity, null);
var newValue = property.GetValue(newEntity, null);
if (oldValue != null && newValue != null)
{
if (!oldValue.Equals(newValue))
{
same = false;
break;
}
}
else if ((oldValue == null && newValue != null) || (oldValue != null && newValue == null))
{
same = false;
break;
}
}
return same;
}
公共布尔比较(T旧实体,T新实体)
{
布尔相同=正确;
PropertyInfo[]properties=oldEntity.GetType().GetProperties();
foreach(属性中的PropertyInfo属性)
{
var oldValue=property.GetValue(oldEntity,null);
var newValue=property.GetValue(newEntity,null);
if(oldValue!=null&&newValue!=null)
{
如果(!oldValue.Equals(newValue))
{
相同=错误;
打破
}
}
else如果((oldValue==null&&newValue!=null)| |(oldValue!=null&&newValue==null))
{
相同=错误;
打破
}
}
返回相同的值;
}
尝试筛选出EntityObject
类型和EntityCollection
属性
var properties = oldEntity.GetType().GetProperties().
Where(pi => !(pi.PropertyType.IsSubclassOf(typeof(EntityObject))
|| pi.PropertyType.IsSubclassOf(typeof(EntityCollection));
利用@Eranga和我的建议,我找到了一个可行的解决方案 由于根对象中的某些属性是GenericType,因此需要两个不同的if语句。仅当当前属性是EntityCollection时跳过它
public bool CompareEntities<T>(T oldEntity, T newEntity)
{
bool same = true;
PropertyInfo[] properties = oldEntity.GetType().GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance)
.Where(pi => !(pi.PropertyType.IsSubclassOf(typeof(EntityObject)))
&& !(pi.PropertyType.IsSubclassOf(typeof(EntityReference)))
).ToArray();
foreach (PropertyInfo property in properties)
{
if (property.PropertyType.IsGenericType)
{
if (property.PropertyType.GetGenericTypeDefinition() == typeof(EntityCollection<>))
{
continue;
}
}
var oldValue = property.GetValue(oldEntity, null);
var newValue = property.GetValue(newEntity, null);
if (oldValue != null && newValue != null)
{
if (!oldValue.Equals(newValue))
{
same = false;
break;
}
}
else if ((oldValue == null && newValue != null) || (oldValue != null && newValue == null))
{
same = false;
break;
}
}
return same;
}
公共布尔比较(T旧实体,T新实体)
{
布尔相同=正确;
PropertyInfo[]properties=oldEntity.GetType().GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance)
.Where(pi=>!(pi.PropertyType.IsSubclassOf(typeof(EntityObject)))
&&!(pi.PropertyType.IsSubclassOf(typeof(EntityReference)))
).ToArray();
foreach(属性中的PropertyInfo属性)
{
if(property.PropertyType.IsGenericType)
{
if(property.PropertyType.GetGenericTypeDefinition()==typeof(EntityCollection))
{
继续;
}
}
var oldValue=property.GetValue(oldEntity,null);
var newValue=property.GetValue(newEntity,null);
if(oldValue!=null&&newValue!=null)
{
如果(!oldValue.Equals(newValue))
{
相同=错误;
打破
}
}
else如果((oldValue==null&&newValue!=null)| |(oldValue!=null&&newValue==null))
{
相同=错误;
打破
}
}
返回相同的值;
}
让自己变得容易,然后改为这样做
PropertyInfo[]properties=oldEntity.GetType().GetProperties(pi=>pi.PropertyType.NameSpace==“系统”).ToArray() 这些实体是POCO还是从
EntityObject
派生的?EntityCollection需要设置一个类型。很抱歉试图找出stackoverflow:)我改为:PropertyInfo[]properties=oldEntity.GetType().GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance)。其中(pi=>!(pi.PropertyType.IsSubclassOf(typeof(EntityObject))&&!(pi.PropertyType.IsSubclassOf(typeof(EntityReference))).ToArray()现在只需要排除EntityCollection。我现在的问题是如何从属性中删除EntityCollection对象list@curiousg修改Where
条件以过滤掉EntityCollection
。你太棒了。这解决了我自己的问题:)