Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3999276
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T07:38:12+00:00 2026-05-20T07:38:12+00:00

I’m trying to make a very rudimentary generic object printer for debugging, inspired by

  • 0

I’m trying to make a very rudimentary generic object printer for debugging, inspired by the awesomeness you get in LinqPad.

Below is the pseudocode for my print function. My reflection-foo is kind of weak at the moment and I’m struggling to deal with the case when the object is an ILookup, as I’d like enumerate the lookup, printing each key alongside its associated collection.

ILookup doesn’t have a non-generic interface and it doesn’t implement IDictionary, so I’m kind of stuck at the moment, as I can’t say o as ILookup<object,object>… For that matter, I’d like to know how to delve into any generic interface…suppose I’d like to have a special case 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's some object, reflect the properties+values
    {
        //reflectiony stuff
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T07:38:12+00:00Added an answer on May 20, 2026 at 7:38 am

    I’m not sure what you’re trying to accomplish exactly, but to answer your specific question, you can use reflection like this:

    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    
    }
    

    I’ve tried to write it in such a way that you can write the printing logic in a strongly-typed manner with generics. If you prefer, you can instead do even more reflection to get the key and values out of each IGrouping<,> in the lookup.

    EDIT: By the way, if you are on C# 4, you can replace the entire body of the if statement with:

    PrintLookup((dynamic)obj);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Does anyone know how can I replace this 2 symbol below from the string
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
Seemingly simple, but I cannot find anything relevant on the web. What is the
this is what i have right now Drawing an RSS feed into the php,
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.