2016-06-08 56 views
3

我正在做跨數據庫比較(有大約20個字段),我定義了一個具體類型來處理比較。循環遍歷定義的具體類型的成員?

如果我的比較失敗,我想遍歷catch塊中的各個項目,併爲用戶提供列表錯誤。下面,我通過前幾個變量手動完成了它。是否有更有效的方法來循環遍歷所有20個字段?我開始用一個foreach(對象objectItem ..但不知道這是一條正確的方式

任何想法或急需的指導

 try { 

     CollectionAssert.IsSubsetOf(orgs, members, "Error Matching testlist Fields"); 

      } 

     catch 
     { 
      //OrgID 
      var sourceOrgID = orgs.Select(o => o.OrgID); 
      var destOrgID = members.Select(o => o.OrgID); 
      var errorList1 = sourceOrgID.Except(destOrgID); 
      string failedTests = null; 
      failedTests = string.Join("\n", errorList1); 
      Assert.IsTrue(0 == failedTests.Length, "The following Org IDs are not contained in the source: \n" + failedTests); 

      //DealerCode 
      var sourceDealerCode = orgs.Select(o => o.DealerCode); 
      var destDealerCode = members.Select(o => o.DealerCode); 
      var errorList2 = sourceDealerCode.Except(destDealerCode); 
      failedTests = null; 
      failedTests = string.Join("\n", errorList2); 
      Assert.IsTrue(0 == failedTests.Length, "The following Dealer Codes are not contained in the source: \n" + failedTests); 


      //orgkey 
      var sourceOrgKey = orgs.Select(o => o.OrgKey); 
      var destOrgKey = members.Select(o => o.OrgKey); 
      var errorList3 = sourceOrgKey.Except(destOrgKey); 
      failedTests = null; 
      failedTests = string.Join("\n", errorList3); 
      Assert.IsTrue(0 == failedTests.Length, "The following Org Keys are not contained in the source: \n" + failedTests); 

回答

0

你需要思考這樣做:?

Type type = obj.GetType(); 
PropertyInfo[] properties = type.GetProperties(); 

foreach (PropertyInfo property in properties) 
{ 
    GetColumn(orgList,property.Name); 
} 



var names = items.Select(x => x.GetType().GetProperty("prpname").GetValue(x)); 


public IEnumerable<object> GetColumn(List<Item> items, string columnName) 
{ 
    var values = items.Select(x => 
x.GetType().GetProperty(columnName).GetValue(x));//u can put your test code heere and you can loop it through object properties 
} 

您可以創建一個列表的rslt存儲和結果添加到列表中其他人,你可以寫在日誌輸出文件

0

如果我沒有誤解你的問題,你能做些什麼像這樣利用shouldly

[TestMethod] 
    public void UnitTestExample() 
    { 
     var orgs = new Organisation 
     { 
      Ids = new List<int>{ 1,3 }, 
      DealerCodes = new List<string> { "foo","bar"} 
     }; 

     var members = new Organisation 
     { 
      Ids = new List<int> { 1,2,3 }, 
      DealerCodes = new List<string> { "foo", "bar", "buzz" } 
     }; 

     orgs.ShouldSatisfyAllConditions(
      () => orgs.Ids.ShouldBe(members.Ids, ignoreOrder: true), 
      () => orgs.DealerCodes.ShouldBe(members.DealerCodes, ignoreOrder: true) 
      ); 
    } 

產生的輸出是:

enter image description here

你仍然必須指定要檢查內ShouldSatisfyAllConditions每一個屬性,但shouldly做所有繁重周圍比較列表並輸出差異。

0

我認爲:

  1. orgsIQueryable<T1>
  2. membersIQueryable<T2>
  3. typeof(T1) == typeof(T2)

如果是這樣,該功能可以幫助:

private static void CompareRecords<TEntity>(IQueryable<TEntity> orgs, IQueryable<TEntity> members) 
{ 
    var entityType = typeof (TEntity); 
    var selectMethod = typeof (Queryable).GetMethods().First(x => x.Name == "Select"); 
    var exceptMethod = typeof(Queryable).GetMethods().First(x => x.Name == "Except"); 
    var toArrayMethod = typeof (Enumerable).GetMethods().First(x => x.Name == "ToArray"); 

    foreach (var property in entityType.GetProperties()) 
    { 
    var paramExpr = Expression.Parameter(entityType, "x"); 
    var propExpr = Expression.Property(paramExpr, property.Name); 
    var delegateType = typeof (Func<,>).MakeGenericType(entityType, property.PropertyType); 
    var lambdaExpr = Expression.Lambda(delegateType, propExpr, paramExpr); 

    var parameterizedSelectMethod = selectMethod.MakeGenericMethod(entityType, property.PropertyType); 
    var parameterizedExceptMethod = exceptMethod.MakeGenericMethod(property.PropertyType); 

    var source = parameterizedSelectMethod.Invoke(null, new object[] {orgs, lambdaExpr}); 
    var dest = parameterizedSelectMethod.Invoke(null, new object[] {members, lambdaExpr}); 

    var errorList = parameterizedExceptMethod.Invoke(null, new[] {source, dest}); 

    var errorListArray = toArrayMethod.MakeGenericMethod(property.PropertyType).Invoke(null, new[] {errorList}); 

    var failedTests = string.Join("\n", ((IEnumerable)errorListArray).Cast<object>().Select(x => x.ToString())); 
    Assert.IsTrue(0 == failedTests.Length, $"The following {property.Name} are not contained in the source: \n{failedTests}"); 
    } 
}