How to determine if an object is an ILookup <,> and print it?

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
    }
}
+3
3

, , , :

public static void PrintIfLookup(object obj)
{
    if (obj == null)
        throw new ArgumentNullException("obj");

    // Find first implemented interface that is a constructed version of
    // ILookup<,>, or null if no such interface exists.
    var lookupType = obj
                    .GetType()
                    .GetInterfaces()
                    .FirstOrDefault
                     (i => i.IsGenericType &&
                           i.GetGenericTypeDefinition() == typeof(ILookup<,>));

    if (lookupType != null)
    {
        // It is an ILookup<,>. Invoke the PrintLookup method
        // with the correct type-arguments.

        // Method to invoke is private and static.
        var flags = BindingFlags.NonPublic | BindingFlags.Static;

        // Assuming the containing type is called Foo.
        typeof(Foo).GetMethod("PrintLookup", flags)
                   .MakeGenericMethod(lookupType.GetGenericArguments())
                   .Invoke(null, new[] { obj });
    }

}

private static void PrintLookup<TKey, TElement>(ILookup<TKey, TElement> lookup)
{
    // TODO: Printing logic    
}

, . , , IGrouping<,> .

EDIT: , # 4, if :

PrintLookup((dynamic)obj);
+4

.

void Print(IDictionary dict)
{
    foreach (var key in dict.Keys)
    {
        var value = dict[key];
        Print(key + " " + value);
    }
}

void Print(object o)
{
    if (o == null || o.GetType().IsValueType || o is string)
    {
        Console.WriteLine(o ?? "*nil*");
        return;
    }
}

void Print(string s)
{
    Console.WriteLine(s);
}

void Print(IEnumerable ie)
{
    foreach (dynamic obj in ie)
    {
        Print(obj);
    }
}
+1

, , :

var objType = o.GetType();

// get the ILookup<,> interface type (if the type implements it)
var lookupInterface = objType.GetInterface("System.Linq.ILookup`2");

// the type implemented the interface if returned non-null value
var isILookup = lookupInterface != null;

type_name`generic_parameter_count

:

type_name`generic_parameter_count[type_name_1,...,type_name_n]

ILookup<,> , :

System.Linq.ILookup`2

, .

0
source

Source: https://habr.com/ru/post/1794052/


All Articles