开发者

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

开发者 https://www.devze.com 2023-01-21 21:41 出处:网络
If I have an object that among other things is an IEnumerable and I dump this object I get the enumerated values.

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开发者_开发百科"; }
}


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


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号