jeudi 4 août 2016

Type "object {System.Collections.Generic.List

I am using reflection to get a List and trying to pass it into a delegate that receives a List.

However, when I use reflection the type of my list is:

object {System.Collections.Generic.List<T>}

And when I pass it to the delegate (of generics) I get an exception because it is expecting the type:

System.Collections.Generic.List<T>

Just to confirm that this really the problem, I made a direct cast to List<RealTClass> and it worked. But, in my code I do not want to make this unnecessary cast... and also because I am using generics.

Question #1: Why the reflection returns the object as type: object { X } ?

Question #2: How can I "remove" the object { X } part from the type? Basically, I need a solution for this problem....

Thanks.

UPDATE #1: some code...

//METHOD receives 'obj' and 'includes'
T obj
Expression<Func<T, object[]>> includes = null
...
if (res && includes != null)
{
    var array = includes.Body as NewArrayExpression;
    if (array != null)
    {
        var exps = ((IEnumerable<object>)array.Expressions).ToArray();          
        for (var i = 0; i < exps.Length; i++)
        {
            var tartetListProperty = (exps[i] as MemberExpression).Member as PropertyInfo;
            var navigationPropertyForList = tartetListProperty.GetCustomAttributes(typeof(NavigationPropertyForList)) as NavigationPropertyForList[];
            if (navigationPropertyForList == null || navigationPropertyForList.Length == 0) continue;
            var navigationPropertyForListString = navigationPropertyForList[0].TargetPropertyName;
            if (tartetListProperty == null) continue;
            var list = tartetListProperty.GetValue(obj);    // WHERE I USE REFLECTION TO GET THE LIST
            var listOfType = list.GetType().GetGenericArguments()[0];

            var repDNI = uow.GetRepositoryDeleteNotIncludedAsyncByType(listOfType);
            await repDNI(list, navigationPropertyForListString, obj.Id);  obj.Id); // THIS IS WHERE IT FAILS

            if (!res) break;
        }
    }
}





Aucun commentaire:

Enregistrer un commentaire