问题
It is very weird problem.
I have ProfileBase class and created a list from it
List<ProfileBase> profileLists = ....
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);
but I cant cast like that
var item = (profileLists[indexPath.Row] as QuickSearchProfile);
or like that
var item = (QuickSearchProfile)profileLists[indexPath.Row];
in both situation, item returns null. Actually when I put a breakpoint, I can see that profileLists[indexPath.Row]'s type is QuickSearchProfile
Where is my mistake?
回答1:
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
来源:https://stackoverflow.com/questions/26899487/could-not-resolve-type-issue-while-i-am-casting-a-custom-class-in-c-sharp