lundi 9 février 2015

How to distinguish Navigation Properties from regular ones when using GetProperties?

Is there a way to distinguish between a regular collection property and a navigation property on an Entity-Framework class (Database-First)?


I'm currently checking if the object is ICollection and IsVirtual but I feel this could possibly trigger on a regular property that someone has declared as a virtual collection.


Question: Are there any other ways to distinguish Navigation Properties from others?


Context: I'm using this to compare values of any object, but I want it to ignore navigation properties (to ignore circular-references among other things).



foreach (var item in (IEnumerable)obj)
{
list2.MoveNext();
var item2 = list2.Current;
foreach (PropertyInfo propInfo in item.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance))
{
Object v1 = propInfo.GetValue(item);
Object v2 = propInfo.GetValue(item2);

Primitive = (v1 == null && v2 == null) || IsPrimitive(v1.GetType());

if (Primitive)
{
Assert.AreEqual(v1, v2);
}
else
{
// Ignore Navigation Properties
if (v1 is ICollection && propInfo.GetGetMethod().IsVirtual) continue;
CompareObjects(v1, v2);
}
}
}





Aucun commentaire:

Enregistrer un commentaire