Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I dump the normal properties on an IEnumerable in Linqpad

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"; }
}
like image 736
David Mårtensson Avatar asked Oct 16 '25 14:10

David Mårtensson


2 Answers

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();
    }   
}

If you put this extension method into "My Extensions", it will be available to all 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 ();
   }
}
like image 108
Joe Albahari Avatar answered Oct 19 '25 04:10

Joe Albahari


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.

like image 35
Ron Warholic Avatar answered Oct 19 '25 05:10

Ron Warholic



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!