在转储OnDemand / DumpContainer时排除忽略的参数

问题描述

编辑-自v6.11.11起已修复!

我试图使用Util.ondemand依赖于flag属性,以两种不同的方式转储结果集,结果发现结果集无法正常工作。我可以用这个小例子来重现它:

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

输出

    Test
    UserQuery+Test

    On Demand Exclude Prop1

点击Util.ondemand链接后:

    Test
    UserQuery+Test

    Test
    UserQuery+Test
    Prop1 One

预期结果:

    Test
    UserQuery+Test

    Test
    UserQuery+Test

我相当确定这与Util.ondemand没有直接关系,但更可能是DumpContainer的问题,因为这也会产生相同的排除结果:

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

在这里做错什么了吗?有潜在的解决方法吗?

我没有检查所有参数,但是我已经确认collapseto在以这种方式调用时也有异常行为,可能还有其他情况。

解决方法

我认为不可能。我的理解是Dump(exclude:"")从调用该方法的对象中排除属性。在您的情况下,您可以在没有LINQPad.DumpContainer属性的Prop1对象上运行它。我无法从Prop1中排除DumpContainer.Content

清洁和推荐的解决方案是覆盖ToDump方法:


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

否则,如果您不想编写其他方法,将首先将其转换为ExpandoObject,这将导致一个难看的衬里:

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

这不是理想的行为:unzip -p ./build/app/outputs/apk/debug/app-debug.apk lib/arm64-v8a/libvision_utils.so > ./build/temp-libvision_utils.sobloaty ./build/temp-libvision_utils.so --debug-file=./build/vision_utils/intermediates/stripped_native_libs/debug/out/lib/arm64-v8a/libvision_utils.so -d compileunits之类的选项应该对转储容器的内容起作用,而不是对容器本身起作用。我将修复下一个LINQPad版本。同时,建议的解决方法看起来不错。