Compare two List <enum> objects in C #

I have two type dictionaries:

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

I want to compare them, where are several entries List<Enum>, how can I compare these objects List<Enum>. See the following code example:

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

Filling Dictionaries:

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...
    }
}

This does not match if I did not provide the exact Enum ie IEnumerable<Days>, but I need common code to work for any Enum.

So far I have found the following comparison method, but I need a general comparison instruction, since I do not know all Enums:

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;
    }
}
+4
source share
1 answer

First enter the type of general arguments with Type.GetGenericArguments(), then you can call IsEnumon that.

So, something like

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;
    } 
+3

All Articles