2012-05-01 2 views
1

을 반대하지 객체 내용 평등을 확인하는 방법 : 나는 IEnumerable을 목록으로 두 개의 서로 다른 데이터 소스에서 항목을 끌어하고나는 이런 식으로 뭔가 보이는 엔티티 프레임 워크 법인이 어떻게 참조 평등

class ListItemEtlObject 
{ 
    public int ID { get; set; } 
    public string ProjectName { get; set; } 
    public string ProjectType { get; set; } 
    public string ProjectCode { get; set; } 
    public string ProjectDescription { get; set; } 
    public string JobNo { get; set; } 
    public string JobDescription { get; set; } 
    public bool Include { get; set; } 
} 

합니다. if 문을 사용하지 않고 항목을 비교하여 속성 값 사이에 차이가 있는지 확인한 다음 일치하지 않으면 속성 값을 설정하는 방법은 무엇입니까? 아이디어는 목록을 동기화 된 상태로 유지하는 것입니다. 또한 목록 A는 ID 값이 설정되어 있고 목록 B는 설정되어 있지 않습니다. 난 그냥 당신이 다음 값을 기반으로 평등 IEquatable<T>을 구현하는 것입니다 지원하기 위해 최선의 선언 방법을 소스 객체의 제어가있는 경우

if(objectA.ProjectName != objectB.ProjectName) 
{ 
    objectA.ProjectName = objectB.ProjectName; 
} 
+0

당신은 반사를 사용하여 비교 기능을 만들 수에 대해 내가 한 테스트의 몇 가지 있습니다. 데이터 유형을 확인하고 그에 대한 비교 코드를 작성하십시오. 그것은 단지 충분히 좋을 수도 있습니다. – CodingBarfield

답변

3

의 무리보다이 할 수있는 더 나은 방법이있다 생각합니다. 유감스럽게도 모든 검사를 열거해야하지만 실제 객체 정의 위치에서 한 번 수행되므로 코드 기반 전체에서 반복 할 필요가 없습니다.

class ListItemEtlObject : IEquatable<ListITemEtlObject> 
{ 
    ... 
    public void Equals(ListITemEtlObject other) { 
    if (other == null) { 
     return false; 
    } 
    return 
     ID == other.ID && 
     ProjectName == other.ProjectName && 
     ProjectType == other.ProjectType && 
     ... ; 
    } 
} 

또한 당신은 객체 유형에 항등 연산자를 오버로드와 소비자가 단순히 ListItemEtlObject 인스턴스에 !===를 사용하여 값 평등 대신 참조 평등을 얻을 수 있도록 선택할 수 있습니다.

public static bool operator==(ListItemEtlObject left, ListItemEtlObject right) { 
    return EqualityComparer<ListItemEtlObject>.Default.Equals(left, right); 
} 
public static bool operator!=(ListItemEtlObject left, ListItemEtlObject right) { 
    return !(left == right); 
} 
2

가장 쉬운 방법은 많이 GetHashCode처럼 특정 해시를 계산하고, 두 개의 인스턴스가 동일한 해시를 계산이라면, 그들은 동등라고 할 수있는 클래스의 방법을 제공하는 것이다.

1
당신은 반사를 사용하여 단순화 할 수

=)

public virtual void SetDifferences(MyBaseClass compareTo) 
    { 
     var differences = this.GetDifferentProperties(compareTo); 

     differences.ToList().ForEach(x => 
     { 
      x.SetValue(this, x.GetValue(compareTo, null), null); 
     }); 
    } 

    public virtual IEnumerable<PropertyInfo> GetDifferentProperties(MyBaseClass compareTo) 
    { 
     var signatureProperties = this.GetType().GetProperties(); 

     return (from property in signatureProperties 
       let valueOfThisObject = property.GetValue(this, null) 
       let valueToCompareTo = property.GetValue(compareTo, null) 
       where valueOfThisObject != null || valueToCompareTo != null 
       where (valueOfThisObject == null^valueToCompareTo == null) || (!valueOfThisObject.Equals(valueToCompareTo)) 
       select property); 
    } 

그리고 여기 당신이

[TestMethod] 
    public void CheckDifferences() 
    { 
     var f = new OverridingGetHashCode(); 
     var g = new OverridingGetHashCode(); 

     f.GetDifferentProperties(g).Should().NotBeNull().And.BeEmpty(); 

     f.Include = true; 
     f.GetDifferentProperties(g).Should().NotBeNull().And.HaveCount(1).And.Contain(f.GetType().GetProperty("Include")); 

     g.Include = true; 
     f.GetDifferentProperties(g).Should().NotBeNull().And.BeEmpty(); 

     g.JobDescription = "my job"; 
     f.GetDifferentProperties(g).Should().NotBeNull().And.HaveCount(1).And.Contain(f.GetType().GetProperty("JobDescription")); 
    } 

    [TestMethod] 
    public void SetDifferences() 
    { 
     var f = new OverridingGetHashCode(); 
     var g = new OverridingGetHashCode(); 

     g.Include = true; 
     f.SetDifferences(g); 
     f.GetDifferentProperties(g).Should().NotBeNull().And.BeEmpty(); 

     f.Include = true; 
     g.Include = false; 
     f.SetDifferences(g); 
     f.GetDifferentProperties(g).Should().NotBeNull().And.BeEmpty(); 
     f.Include.Should().BeFalse(); 
    } 
관련 문제