4

So I'm trying to write a C# function print_r() that prints out information about a value passed much in the same way that the PHP print_r() function works.

What I'm doing is taking in an object as an input in to the function, and depending on what type it is, I'll output the value, or loop through an array and print out the values inside the array. I have no problem printing out basic values, but when I try to loop through the object if I detect it is an array, I get an error from C# saying "Error 1 foreach statement cannot operate on variables of type 'object' because 'object' does not contain a public definition for 'GetEnumerator'".

Now I'm assuming this is just because object doesn't implement IEnumerable<>, but is there any way that I can process this taking in the input as type object?

This is my current code for the function (the IEnumerable<> part is blank in terms of content, but this is the code that is giving me an error.

Thanks.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Reflection;

namespace ConsoleApplication1
{
    class Program
    {
        static void print_r(object val)
        {
            if (val.GetType() == typeof(string))
            {
                Console.Write(val);
                return;
            }
            else if (val.GetType().GetInterface(typeof(IEnumerable).FullName) != null)
            {
                foreach (object i in val)
                {
                    // Process val as array
                }
            }
            else
            {
                Console.Write(val);
                return;
            }
        }

        static void Main(string[] args)
        {
            int[] x = { 1, 4, 5, 6, 7, 8 };
            print_r(x);
            Console.Read();
        }
    }
}
John McCarthy
  • 41
  • 1
  • 1
  • 3
  • 1
    @scibuff this topic is the 1st result on google. – dvdmn Oct 14 '13 at 18:12
  • @dvdnhm yeah, because in 18 months link ranks didn't change - please think before you post complete nonsense – scibuff Oct 14 '13 at 21:30
  • 2
    @scibuff what I was trying say is "it would be helpful in the future if you put the link in here instead of describe where it is". That way I would get something useful from your comment. also your comment would not be a "nonsense" after 18 months. – dvdmn Oct 15 '13 at 02:46
  • 1
    @scibuff Ironically now the first result is this question – BotMaster3000 Jun 27 '17 at 08:22
  • That's what dvdmn was trying to say – Matt Kenefick Oct 15 '19 at 18:02
  • Take a look at this reply: https://stackoverflow.com/questions/852181/c-printing-all-properties-of-an-object – LoxLox Oct 15 '21 at 00:14

3 Answers3

5

val is declared as an Object. After checking if it's an IEnumerable (which you can do simply with is, as shown, but this works also with your original code) you have to cast it explicitly

else if (val is IEnumerable)
{
    var e = val as IEnumerable;
    foreach (var i in e)
    {
        Console.WriteLine(i.ToString());
    }
}
Paolo Falabella
  • 24,914
  • 3
  • 72
  • 86
0

There is LINQPad that has Dump() extension method, but you can use it only in LINQPad
We can write our own extension method to dump any object to html and view it in browser.
You need to reference LINQPad.exe

public static class Extension
    {
        public static void Dump<T>(this T o)
        {
            string localUrl = Path.GetTempFileName() + ".html";
            using (var writer = LINQPad.Util.CreateXhtmlWriter(true))
            {
                writer.Write(o);
                File.WriteAllText(localUrl, writer.ToString());
            }
            Process.Start(localUrl);
        }
    }
diimdeep
  • 1,096
  • 17
  • 27
0

You would have to use reflection to do this I think, I want a similiar function and ouputted my objects into tables using reflection.

I dont have the code to hand but found the basis of my solution here!

Community
  • 1
  • 1
Ian Jowett
  • 189
  • 18