编辑-这已经修正为6.11.11!版本
我试图以两种不同的方式转储结果集,这取决于使用Util.OnDemand的标志属性,发现它不像预期的那样工作。我用这个小例子再现了它:
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也有错误行为,其他可能也是如此。
发布于 2020-09-12 01:59:50
这不是理想的行为:像include和exclude这样的选项应该对转储容器的内容进行操作,而不是对容器本身操作。我将在下一个LINQPad构建中得到一个修复。与此同时,建议的解决方案看起来不错。
发布于 2020-09-11 21:24:08
我不相信那是可能的。我的理解是,Dump(exclude:"")从调用方法的对象中排除了属性。在您的示例中,您在没有Prop1属性的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()https://stackoverflow.com/questions/63854056
复制相似问题