问题描述
这是非常奇怪的问题。
It is very weird problem.
我有ProfileBase类并从中创建了一个列表
I have ProfileBase class and created a list from it
List<ProfileBase> profileLists = ....
是QuickSearchProfile也来自ProfileBase。
well after i add items to this list from another list name is QuickSearchProfile which is derived from ProfileBase also.
var _list = new List<ProfileBase>();
_list.AddRange((IEnumerable<QuickSearchProfile>)list);
但我不能这样投入
var item = (profileLists[indexPath.Row] as QuickSearchProfile);
或类似
var item = (QuickSearchProfile)profileLists[indexPath.Row];
在这两种情况下,项目返回null。事实上,当我放一个断点,我可以看到profileLists [indexPath.Row]的类型是QuickSearchProfile
in both situation, item returns null. Actually when I put a breakpoint, I can see that profileLists[indexPath.Row]'s type is QuickSearchProfile
我的错误在哪里?
推荐答案
保存项目uncasted,并获取其完整的typename并输出,然后获取typeof(QuickSearchProfile),并做同样的。 CLR是说他们不一样 - 我相信它
save item uncasted and get its full typename and output it, then get typeof(QuickSearchProfile) and do the same. THe CLR is sayin they arent the same - I believe it
这篇关于无法解析类型:当我在c#中投射自定义类时发生问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!