asawyer
asawyer

Reputation: 17808

Exclude argument ignored when Dump'ing an OnDemand / DumpContainer

Edit - This has been fixed as of v6.11.11!

I was trying to dump a result set in two different ways depending a flag property using Util.OnDemand and found that it was not working as expected. I was able to reproduce it with this small example:

    void Main()
    {
        var i = new Test() { Prop1 = "One" };
        i.Dump(exclude:"Prop1");
        Util.OnDemand("On Demand Exclude Prop1", ()=>i).Dump(exclude:"Prop1");
    }
    
    public class Test
    {
        public string Prop1 { get; set; }
    }

Output:

    Test
    UserQuery+Test

    On Demand Exclude Prop1

After clicking the Util.OnDemand link:

    Test
    UserQuery+Test

    Test
    UserQuery+Test
    Prop1 One

Expected Result:

    Test
    UserQuery+Test

    Test
    UserQuery+Test

I'm fairly sure this is not related to Util.OnDemand directly but more likely a problem with DumpContainer as this also produces the same non-excluded result:

    new DumpContainer(i).Dump(exclude:"Prop1");

Is there something I'm doing wrong here? Any potential work arounds?

I haven't checked every parameter but I have confirmed collapseTo is also misbehaving when invoked in this fashion, potentially others are as well.

Upvotes: 0

Views: 126

Answers (2)

Joe Albahari
Joe Albahari

Reputation: 30964

This is not ideal behavior: options such as include and exclude should operate on the content of the dump container, not the container itself. I'll get a fix into the next LINQPad build. In the meantime, the suggested workarounds look good.

Upvotes: 1

lukaszberwid
lukaszberwid

Reputation: 1177

I don't believe that's possible. My understanding is Dump(exclude:"") excludes property from the object that the method was called on. In your case you run it on LINQPad.DumpContainer object that doesn't have Prop1 property. I was not able to exclude Prop1 from DumpContainer.Content.

Clean and recommended solution is to overwrite ToDump method:


public class Test
{
    public string Prop1 { get; set; }

object ToDump()
{
   IDictionary<string,object> custom = new System.Dynamic.ExpandoObject();

   var props = GetType().GetProperties (BindingFlags.Public | BindingFlags.Instance)
       .Where (p => p.Name != "Prop1");

   foreach (var prop in props)
      custom[prop.Name] = prop.GetValue (this);
      
   return custom;
}
}

Other way, if you don't want to write additional methods, would be to convert it to ExpandoObject first, which will result in ugly one liner:

Util.OnDemand("On Demand Exclude Prop1", () => (Util.ToExpando(i, exclude:"Prop1"))).Dump()

Upvotes: 2

Related Questions