jeudi 7 décembre 2017

.Net dynamic list conversion

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]'





Aucun commentaire:

Enregistrer un commentaire