2015-07-10 85 views
4

我有兩個詞典,如:比較兩個列表<enum>對象在C#中

Dictionary<string, object> dict1 = new Dictionary<string, object>(); 
Dictionary<string, object> dict2 = new Dictionary<string, object>(); 

我想對它們進行比較,其中一些條目List<Enum>,我怎麼能比較這些List<Enum>對象。請看下面的示例代碼:

enum Days { Monday, Tuesday, Wednesday } 
enum Colors { Red, Green, Blue } 
enum Cars { Acura, BMW, Ford } 

填充字典:

List<Days> lst1 = new List<Days>(); 
List<Days> lst2 = new List<Days>(); 

lst1.Add(Days.Monday); lst1.Add(Days.Tuesday); 
lst2.Add(Days.Monday); lst2.Add(Days.Tuesday); 

dict1.Add("DayEnum", lst1); 
dict2.Add("DayEnum", lst2); 

foreach (KeyValuePair<string, object> entry in dict1) 
{ 
    var t1 = dict1[entry.Key]; 
    var t2 = dict2[entry.Key]; 

    if (dict1[entry.Key].GetType().IsGenericType && Compare(dict1[entry.Key], dict2[entry.Key])) 
    { 
       // List elements matches... 
    } 
    else if (dict1[entry.Key].Equals(dict2[entry.Key])) 
    { 
       // Other elements matches... 
    } 
} 

不匹配,除非我提供確切的枚舉即IEnumerable<Days>,但我需要一個通用的代碼,以用於任何枚舉工作。

到目前爲止,我發現下面的方式來比較,但我需要一個通用的比較語句,我不知道所有的枚舉:

private static bool Compare<T>(T t1, T t2) 
{ 
    if (t1 is IEnumerable<T>) 
    { 
     return (t1 as IEnumerable<T>).SequenceEqual(t2 as IEnumerable<T>); 
    } 
    else 
    { 
     Type[] genericTypes = t1.GetType().GetGenericArguments(); 
     if (genericTypes.Length > 0 && genericTypes[0].IsEnum) 
     { 
      if (genericTypes[0] == typeof(Days)) 
      { 
       return (t1 as IEnumerable<Days>).SequenceEqual(t2 as IEnumerable<Days>); 
      } 
      else if (genericTypes[0] == typeof(Colors)) 
      { 
       return (t1 as IEnumerable<Colors>).SequenceEqual(t2 as IEnumerable<Colors>); 
      } 
     } 

     return false; 
    } 
} 
+0

爲什麼你需要測試它是否是特定的枚舉列表?枚舉是值類型,所以你可以測試值類型。 –

+0

@大衛,它不是枚舉比較,它是枚舉比較列表,它不能像數值類型那樣進行比較。 – Asim

+0

那肯定是列表比較問題? –

回答

3

首先得到的一般參數的類型與Type.GetGenericArguments(),那麼你就可以就此致電IsEnum

所以,像

var listType = dict1[entry.Key].GetType(); 
if (listType.IsGenericType) 
{ 
    var typeArguments = listType.GetGenericArguments(); 
    //if you only have List<T> in there, you can pull the first one 
    var genericType = typeArguments[0]; 
    if (genericType.IsEnum) { 
     // List elements matches... 
    }     
} 

在回答您的意見,如果你想比較2名列表,你可以做以下。因爲在字典中有列表作爲對象,所以最好創建自己的方法來檢查每個元素是否與另一個列表中的元素相同,因爲對於SequenceEqual,您必須知道類型。

 var listType = dict1[entry.Key].GetType(); 
     var secondListType = dict2[entry.Key].GetType(); 
     if (listType.IsGenericType && secondListType.IsGenericType) 
     { 
      //if you only have List<T> in there, you can pull the first one 
      var genericType = listType.GetGenericArguments()[0]; 
      var secondGenericType = secondListType.GetGenericArguments()[0]; 
      if (genericType.IsEnum && genericType == secondGenericType && AreEqual((Ilist)dict1[entry.Key],(Ilist)dict2[entry.Key])) 
      {      
       // List elements matches... 
      } 
     } 

    public bool AreEqual(IList first, IList second) 
    { 
     if (first.Count != second.Count) 
     { 
      return false; 
     } 

     for (var elementCounter = 0; elementCounter < first.Count; elementCounter++) 
     { 
      if (!first[elementCounter].Equals(second[elementCounter])) 
      { 
       return false; 
      } 
     } 
     return true; 
    } 
+0

我已經嘗試過,但沒有找到匹配那些List對象的方法。如果你知道的話請寫出通用的比較聲明。 – Asim

+0

我也更新了有問題的代碼。 – Asim

+0

@Asim我更新了我的答案,包括代碼來比較2個列表 – Stephen