简体   繁体   English

键入“对象{System.Collections.Generic.List <T> }“ VS” System.Collections.Generic.List <T> ”

[英]Type “object {System.Collections.Generic.List<T>}” VS “System.Collections.Generic.List<T>”

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. 为了确认这确实是问题,我直接将其List<RealTClass>List<RealTClass>并成功运行。 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 } ? 问题#1 :为什么反射将对象返回为类型: object { X }

Question #2 : How can I "remove" the object { X } part from the type? 问题2 :如何从类型中“删除” object { X }部分? Basically, I need a solution for this problem.... 基本上,我需要一个解决此问题的方法。

Thanks. 谢谢。

UPDATE #1: some code... 更新#1:一些代码...

//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); // THIS IS WHERE IT FAILS

            if (!res) break;
        }
    }
}

The repDNI object is correct and working if I do the correct casting, the only problem I am having is on getting the list , the type object { X } is surrounding my correct type. 如果我执行正确的转换,则repDNI对象是正确的并且可以正常工作,我遇到的唯一问题是获取list ,类型object { X }包围着我的正确类型。

I was able to make it work by changing the following line: 我可以通过更改以下行使其起作用:

Before: var list = tartetListProperty.GetValue(obj); 之前: var list = tartetListProperty.GetValue(obj);

After: dynamic list = tartetListProperty.GetValue(obj); 之后: dynamic list = tartetListProperty.GetValue(obj);

You could cast to a generic list: 您可以转换为通用列表:

await repDNI((List<T>)list, navigationPropertyForListString, obj.Id);

As far as I know this should resolve your problem while remaining the generic functionality. 据我所知,这应该可以解决您的问题,同时又保留通用功能。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法隐式转换类型&#39;System.Collections.Generic.list <fooClass> &#39;到&#39;System.Collections.Generic.List <T> - Cannot Implicitly convert type 'System.Collections.Generic.list<fooClass>' to 'System.Collections.Generic.List<T> 无法隐式转换类型&#39;System.Collections.Generic.List <MyProduct> &#39;到&#39;System.Collections.Generic.List <T> - Cannot implicitly convert type 'System.Collections.Generic.List<MyProduct>' to 'System.Collections.Generic.List<T> 在System.Collections.Generic.List中的Find() <T> - Find() In a System.Collections.Generic.List<T> System.Collections.Generic.List <T> 需要&#39;1&#39;类型参数 - System.Collections.Generic.List<T> requires '1' type arguments 如何处理类型“ Object {System.Collections.Generic.List <object> }” - How to handle type “Object {System.Collections.Generic.List<object>}” 无法将类型为“System.Collections.Generic.List`1[System.Object]”的 object 转换为类型“System.Collections.Generic.List`1” - Unable to cast object of type 'System.Collections.Generic.List`1[System.Object]' to type 'System.Collections.Generic.List`1[customType] 无法将类型&#39;object [] []&#39;转换为&#39;System.Collections.Generic.List <object> “ - Cannot convert type 'object[][]' to 'System.Collections.Generic.List<object>' 无法将JSON对象反序列化为“System.Collections.Generic.List”类型 - Cannot deserialize JSON object into type 'System.Collections.Generic.List' 无法转换&#39;System.Collections.Generic.List`1类型的对象 - Unable to cast object of type 'System.Collections.Generic.List`1 无法隐式转换类型&#39;System.Collections.Generic.List <AnonymousType#1> &#39;到&#39;System.Collections.Generic.List - Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1>' to 'System.Collections.Generic.List
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM