【问题标题】:Visual Studio - debug huge list of objectsVisual Studio - 调试大量对象
【发布时间】:2015-05-22 07:40:00
【问题描述】:

我正在调试一个应用程序,并且在某个时刻,我有一个包含大量项目的列表,这让我无法一步一步地观察每个元素。我想检查 PropertyName = "XXX" 的元素的属性 Layer 内的值。有什么简单的方法吗?

外层代码:

var metadata = FieldsConfigurationProvider.GetAllFieldsConfiguration();
RequestDocumentTypeModel model = new RequestDocumentTypeModel()
{
    Requester = CurrentUser.Login,
    MetadataItems = metadata.Where(f => !f.IsSystemGenerated).Select(f => new RequestDocumentMetadataItem(f.DocumentModelPropertyName, f.DisplayName, false, f.Layer)).ToList()
};

// BREAKPOINT HERE
// # MORE CODE #

当然我不能使用即时窗口和 LINQ,因为不允许使用 LINQ。我使用的是 Visual Studio 2010,但据我所知,其他版本也有同样的“问题”。

【问题讨论】:

  • 你能不能写一个函数,从输入PropertyName返回你想要的,然后在调试的时候在quickwatch中运行?
  • 您在寻找什么?蒂姆关于使用条件中断的建议可能是正确的 (+1),但我怀疑当你得到一个无论出于何种原因的错误值时,你应该使用它来中断
  • @leppie - 谢谢 :) 这很有帮助。

标签: c# visual-studio


【解决方案1】:

我想检查元素的属性层内的值是什么 属性名称 = "XXX"。有什么简单的方法吗?

是的,您可以指定断点条件。然后它只有在满足条件时才停止。

在你的情况下:

PropertyName == "XXX"

How to: Specify a Breakpoint Condition

请记住,您永远不会指定像 PropertyName = "XXX" 这样的条件,因为它会默默地更改变量值。

【讨论】:

  • 是的,确实如此,但问题是,我不想每次想检查列表中的某些属性时都编写循环。我的代码未处理此项目列表。
  • @fka: 不用改代码,调试时指定断点条件即可。您不需要编译代码。只有在满足条件时才会停止。
  • 我编辑了我的问题。也许我不太了解您的答案,但是当我在指定位置放置断点(当然这条线没有实际评论)并放置 contition: model.MetadataItems.PropertyName == "XXX" 这应该有效吗?我不这么认为,因为 MetadataItems 没有属性 PropertyName - 列表中的项目有。
  • @Fka:所以实际上如果列表中的任何项目有.PropertyName == "XXX",你想停止吗?那么不改变代码确实很困难,因为你不能在调试器中使用 LINQ。我会添加一个局部变量bool containsPropName=model.MetadataItems.Any(i=>i.PropertyName == "XXX");。然后可以指定使用containsPropName的断点条件。
  • 没有。正如我在我的问题中提到的,我只想以简单的方式检查带有PropertyName = "XXX" 的项目的Layer 属性的值是什么。就是这样。
【解决方案2】:

如果您需要一次查看整个列表,请在RequestDocumentMetadataItem 中尝试DebuggerDisplayAttribute MetadataItems

[DebuggerDisplay("DisplayName = {DisplayName} PropertyName = {PropertyName}")]

【讨论】:

    【解决方案3】:

    我可以建议两种解决方法:

    1. 按照此答案中的建议使用 System.Linq.Dynamic 表达式:https://stackoverflow.com/a/2771843/245452
    2. 等待 Visual Studio 2015 (http://blogs.msdn.com/b/visualstudioalm/archive/2014/11/12/support-for-debugging-lambda-expressions-with-visual-studio-2015.aspx)

    【讨论】:

      【解决方案4】:

      您可以尝试在您的对象类型的List 周围编写一个小包装器,并给它一个基于字符串的[] 重载访问器。像这样:

      public class ComplexType
      {
          public string PropertyName { get; set; }
          public string Layer { get; set; }
          public string DisplayName { get; set; }
      }
      
      public class DebuggableList : List<ComplexType>
      {
          public ComplexType this[string key]
          {
              get
              {
                  return this.FirstOrDefault(i => i.PropertyName == key);
              }
          }
      }
      
      class Program
      {
          static void Main(string[] args)
          {
              var myList= new DebuggableList();
      
              myList.Add(new ComplexType { DisplayName = "XXX", Layer = "YYY", PropertyName = "ZZZ" });
              myList.Add(new ComplexType { DisplayName = "AAA", Layer = "BBB", PropertyName = "CCC" });
              myList.Add(new ComplexType { DisplayName = "DDD", Layer = "EEE", PropertyName = "FFF" });
          }
      }
      

      在监视窗口中,您可以使用myList["XXX"] 访问您想要的对象,然后会显示带有PropertyName=="XXX" 的对象。

      【讨论】:

        【解决方案5】:

        也许右键单击代码中的行,选择Breakpoint -> Insert Tracepoint,并使用注释明确指定属性名称,例如:

        然后您可以在输出窗口中检查它。它的美妙之处在于您不需要编译任何额外的代码,并且您可以在运行时随时更改它。

        【讨论】:

          【解决方案6】:

          您可以编写一个带有辅助函数的静态调试类来进行调试。有一些扩展可以帮助在 VS 中进行调试,但我认为没有一个是免费的。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2018-03-06
            • 2016-02-20
            • 2011-02-25
            • 2013-09-18
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多