public static bool PropertiesEqual<T>(this T self, T other, string[] skip)
{
if (self.Equals(other)) return true;
var primitive = (from p in typeof(T).GetProperties()
where !skip.Contains(p.Name)
&& p.PropertyType.IsSimpleType()
select p).ToList();
var rest = (from p in typeof(T).GetProperties()
where !p.PropertyType.IsSimpleType() select p).ToList();
foreach(var pi in rest)
{
var selfValue = pi.GetValue(self, null);
var otherValue = pi.GetValue(other, null);
//var result = selfValue.PropertiesEqual(otherValue);
if (!object.Equals(selfValue, otherValue))
return false;
}
foreach (var pi in primitive)
{
var selfValue = pi.GetValue(self, null);
var otherValue = pi.GetValue(other, null);
return object.Equals(selfValue, otherValue);
}
return true;
}
public static bool IsSimpleType(this Type type)
{
return (type.IsValueType || type.IsPrimitive ||
type == typeof(String) || Convert.GetTypeCode(type) != TypeCode.Object);
}
このメソッドを使用して、エンティティ インスタンスの等価性を比較しています。最初のレベルではうまく機能しますが、rest
(アタッチされたエンティティ) を反復処理して、このメソッド (コメント行) を再帰的に呼び出したいと思います。
問題は、再帰呼び出しで入力されるため、結果がゼロになることですself
。最初のレベルでタイプを検査すると、実際のタイプが得られますが、2 番目のレベルでは. を使用してみましたが、役に立ちませんでした。other
object
primitive
self
this
object
Convert.ChangeType