I am trying to make a very rudimentary model debug object printer, inspired by the awesomeness you get in LinqPad.
Below is the pseudo code for my print function. My reflection-foo is weak now, and I am struggling to deal with the case when the object is ILookup, since I would like to list the search by printing each key along with its associated collection.
ILookup does not have a universal interface and does not implement IDictionary, so Iβm kind of stuck at the moment, because I canβt say o as ILookup<object,object>... In this regard, I'd like to know how to delve into some common interface ... suppose I would like to have a special occasion for CustomObject<,,>.
void Print(object o)
{
if(o == null || o.GetType().IsValueType || o is string)
{
Console.WriteLine(o ?? "*nil*");
return;
}
var dict = o as IDictionary;
if(dict != null)
{
foreach(var key in (o as IDictionary).Keys)
{
var value = dict[key];
Print(key + " " + value);
}
return;
}
//how can i make it work with an ILookup?
//?????????
var coll = o as IEnumerable;
if(coll != null)
{
foreach(var item in coll)
{ print(item); }
return;
}
//else it some object, reflect the properties+values
{
//reflectiony stuff
}
}