views:

28

answers:

3

If I have an object that among other things is an IEnumerable and I dump this object I get the enumerated values.

Is there a way to get Linqpad to list the other properties:

Se example below: Can I get Dump to include Hello and digits properties?

void Main()
{
    var t = new test();
    var d = new Dictionary<string,string> {{"Hello","World"},{"Good by","Sky"}};

    t.Dump();
    d.Dump();
}

// Define other methods and classes here

public class test : IEnumerable
{
    public string Hello { get { return "World"; }}
    public List<int> digits { get { return new List<int> {0,1,2,3,4,5,6,7,8,9}; }}

    public IEnumerator GetEnumerator() { yield return "Hej"; }
}
+1  A: 

As far as I can tell if the object you're trying to dump implements IEnumerable then LINQPad always wants to dump it as an IEnumerable list. Getting rid of the interface correctly shows the Hello and digits properties in the dumped info.

Going from this link it appears you can write your own dump which accomplishes something like LINQPad by enumerating the collection and all it's properties then outputting the whole thing as an XHTML string. I haven't tried this.

Ron Warholic
I accepted this as the answer as it explained the reason why Dump acts as it does. I was primarily looking if there was a setting or parameter I could use.
David Mårtensson
A: 

Use a Serializer?

Json.NET will do all of this for you in a json format.

Newtonsoft.Json.JsonConvert.SerializeObject(t, Newtonsoft.Json.Formatting.Indented)

if you don't want json, then pick a serializer you do want, or you'll just have to do what a serializer would do, use reflection to iterate the properties on the object.

jrwren
+1  A: 

You could write a DumpPayload extension method as follows:

void Main()
{
    var t = new test();
    t.DumpPayload();
}

public static class Extensions
{
    public static void DumpPayload (this IEnumerable o)
    {
        if (o == null)
        {
            o.Dump();
            return;
        }
        var d = new Dictionary<string,object>();        
        foreach (var member in o.GetType().GetProperties())
            try
            {
                d[member.Name] = member.GetValue (o, null);
            }
            catch (Exception ex)
            {
                d[member.Name] = ex;
            }           
        d["AsEnumerable"] = o;
        d.Dump();
    }   
}

This will work as-is in LINQPad, although it might be more convenient to create a class library in VS with this extension method. You can then reference it in LINQPad with F4 and click 'Set as default for new queries'.

Another solution is to implement ICustomMemberProvider in the test class:

public class test : IEnumerable, ICustomMemberProvider
{
    public string Hello { get { return "World"; }}
    public List<int> digits { get { return new List<int> {0,1,2,3,4,5,6,7,8,9}; }}

    public IEnumerator GetEnumerator() { yield return "Hej"; }

    IEnumerable<string> ICustomMemberProvider.GetNames() 
    {
        return "Hello Enumerator".Split();
    }

    IEnumerable<Type> ICustomMemberProvider.GetTypes ()
    {
        return new [] { typeof (string), typeof (IEnumerable) };
    }

    IEnumerable<object> ICustomMemberProvider.GetValues ()
    {
        return new object [] { Hello, this.OfType<Object>() };
    }   
}

Note that if the test class is defined in another assembly, you don't need to reference LINQPad in order to implement ICustomMemberProvider. You can just paste in the following definition into your project and LINQPad will pick it up:

namespace LINQPad
{
   public interface ICustomMemberProvider
   {
      IEnumerable<string> GetNames ();
      IEnumerable<Type> GetTypes ();
      IEnumerable<object> GetValues ();
   }
}
Joe Albahari