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

房东的猫 提交于 2020-01-04 04:21:11

问题


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:


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.




回答2:


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



回答3:


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.



来源:https://stackoverflow.com/questions/3988656/how-can-i-dump-the-normal-properties-on-an-ienumerable-in-linqpad

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!