Iterating List<int> in a Anonymous Type

127 Views Asked by At

I am using Ninject Interception extension to implement SQLLogging. I have a variable called 'param' and it is an Anonymous type as it showed in Watch window (See the picture below). When I do param.ToString() it returned

{Countries = {System.Collections.Generic.List< int >}


var param = invocation.Request.Arguments[0]; 

where invoation is of type Ninject.Extensions.Interception.IInvoation Interface.

What I need to get is a name and its items. So, for this one I want something like "Countries = 36, 124, 826, 840".

How do I iterate through the List to achieve that? Also, notice that the name Countries in this case is not known until run-time. It could be something like

{Ages = {System.Collections.Generic.List< int >}

or

{Subjects={System.Collections.Generic.List< string >}

object in watch window

2

There are 2 best solutions below

1
Stefan Steinegger On

You probably want to write your own visualizer for List<T>. It will be used for all lists, not only the ones of your anonymous types, but it may still be useful for you.

https://msdn.microsoft.com/en-us/library/e2zc529c.aspx

0
Stefan Steinegger On

You could make use of reflection, looping properties, checking the type and if it is a list, iterate the list.