Home .Net dynamic list conversion
Reply: 2

.Net dynamic list conversion

Radu
1#
Radu Published in 2017-12-07 09:03:20Z

I have to do a comparison of 2 lists. The problem is that I don't know of what type the field inside the list are, they can be int, string, decimal, enums or even other objects.

I will know the type only on runtime. I was thinking of creating a list of object and cast them to object the problem is that let's say I have a List<int> and I'm trying to cast it to object it fails.

Another problem is that I know there is a list only on runtime. so on runtime I need to transform the variable of type object to a list.

How can I cast that object to List and how can I cast it to let's say list of objects?

Update:

I have and object and by reflection I'm getting the the property of it with

var oldProperty = property.GetValue(old);
var newProperty = property.GetValue(new);

Once I have the properties values and I can see it's a list I will need to compare those 2. Let's say oldProperty is of type List I've tried to do something like:

var myOldList = (List<object>)oldProperty;

If the cast fails with

Unable to cast object of type 'System.Collections.Generic.List`1[System.Int32]' to type 'System.Collections.Generic.List`1[System.Object]'

Here you have a look of the function i;m trying to create. Please don't mind of null objects(is not in the scope)

 public void SetDifference(object first, object second)
    {
        var properties = first.GetType().GetProperties();
        foreach (PropertyInfo property in properties)
        {
            var oldValue = property.GetValue(first);
            var newValue = property.GetValue(second);
            if (Convert.GetTypeCode(newValue) != TypeCode.Object)
            {
                if (!oldValue.Equals(newValue))
                {
                    result.AddDifference(new PrimitiveComparison()
                    {
                        BeforeValue = oldValue.ToString(),
                        AfterValue = newValue.ToString(),
                        PropertyName = property.Name
                    });
                }
            }
            else
            {
                if (property.PropertyType.Name.Contains("List"))
                {
                    // here fails with the error from above
                    var oldList = (List<object>)oldValue; 
                    var newList = (List<object>)newValue;
                    if (oldList.Count != newList.Count)
                    {
                        result.AddDifference(new PrimitiveComparison()
                        {
                            BeforeValue = oldList.Count.ToString(),
                            AfterValue = newList.Count.ToString(),
                            PropertyName = property.Name + "Count"
                        });
                    }
                    // add the list differences
                    result.AddDifference(SetListDifference(oldList, newList);
                }
                else
                {
                    var diffrence = SetDifference(oldValue, newValue);
                    if (!diffrence.areEqual)
                    {
                        result.AddDifference(diffrence);
                    }
                }

            }
        }


}
Pranay Rana
2#
Pranay Rana Reply to 2017-12-07 09:55:58Z

once you do conversion of you list than you can check both the list have same element or not by using except method of linq, to find both are equal or not

double[] numbers1 = { 2.0, 2.1, 2.2, 2.3, 2.4, 2.5 };
double[] numbers2 = { 2.0, 2.1, 2.2, 2.3, 2.4, 2.5 };

IEnumerable<double> onlyInFirstSet = numbers1.Except(numbers2);
if(onlyInFirstSet.Count() ==0)
 Console.WriteLine("equal");

for primitive type this works fine but for user defined type you need compare implementation.

Check this blog post for comparing list of two different type : Difference between list of user defined types


If you are aware that is going to be IEnumerable type than you can try

 List<object> objlst = (value as IEnumerable<object>).Cast<object>
    ().ToList()

you can try like this

Type t = typeof(obj);

if (t == typeof(List<int>)) {
    var lst= (List<int>)obj;

} else if (t == typeof(List<string>)) {
    var lst = (List<string>)obj;
} else if (t == typeof(List<decimal>)) {
    var lst = (List<decimal>)obj;
} 
else if (t == typeof(List<EnumName>)) {
    var lst = (List<EnumName>)obj;
}
Evk
3#
Evk Reply to 2017-12-07 10:04:12Z

You can just cast your two values to IList and compare them, for example like this:

static bool AreEqual(IList first, IList second) {
    if (first.Count != second.Count)
        return false;
    for (int i = 0; i < first.Count; i++) {
        if (!object.Equals(first[i], second[i]))
            return false;
    }
    return true;
}
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.305396 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO