So, I'm trying to write a C # print_r () function that prints information about a value passed in the same way as the PHP print_r () function.
What I am doing is taking an object as an input to a function, and depending on what type it will be, I will post a value or go through an array and print the values ββinside the array. I have no problem printing basic values, but when I try to loop through an object, if I find that it is an array, I get an error with C #, in which: "Error 1 foreach statement cannot work with variables like" object because "object 'does not contain a public definition for" GetEnumerator ".
Now I assume that this happens only because the object does not implement IEnumerable <>, but is there a way I can treat this take in input as an object of type?
This is my current code for the function (the IEnumerable <> part is empty in content, but this is the code that gives 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) {
source share