C#C
C#2y ago
stigzler

Casting types inline

I'm still not clear on all the routes to this, and which one is best for readability. I'm trying to get
.Count
from a child object of
object sender
. The Type hierarchy is:

DirectDragTreeViewItem.GistViewModel.GistModel.IList<GistFileModel>


Here are a couple of ways I've tried:

int directCastListCount = (GistViewModel)((DirectDragTreeViewItem)sender).DataContext) // wtf!? gave up here
int paternMatchedListCount = ((((sender as DirectDragTreeViewItem).DataContext as GistViewModel).Gist as GistModel).Files as IList<GistFileModel>).Count();

Of course things could be nested deeper. Which is the best way (realise 'best' is ambiguous)
Was this page helpful?