c#winform在运行时检查对象

时间:2015-06-18 08:35:47

标签: c# .net winforms

我想在GUI中运行时显示任意对象的公共属性/值。

是否有winform允许用户在调试模式下查看任何对象的内容?该对象将包含许多词典>能够在运行时扩展和查看这些列表的内容会很高兴。

如果没有,有什么办法可以实现类似的目标吗?

由于

2 个答案:

答案 0 :(得分:4)

您需要做的就是创建一个带有PropertyGrid的表单。然后设置所选对象。

enter image description here

using xxx.CFM.UI.Core;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace xxx.CFM.UI.Forms
{
    /// <summary>
    /// Form used to inspect objects at runtime
    /// </summary>
    public partial class frmInspector : BaseForm
    {
        #region Properties

        /// <summary>
        /// Gets or Sets the 
        /// </summary>
        public object SelectedObject
        {
            get { return propertyGrid.SelectedObject; }
            set { propertyGrid.SelectedObject = value; }
        }

        #endregion Properties

        #region Constructor

        /// <summary>
        /// Constructor
        /// </summary>
        public frmInspector(object objectToInspect)
        {
            try
            {
                InitializeComponent();

                this.SelectedObject = objectToInspect;
            }
            catch (Exception ex)
            {
                UIMessage.DisplayError(ex);
            }
        }

        #endregion Constructor

        #region Events

        /// <summary>
        /// Closes the form
        /// </summary>
        /// <param name="sender">object</param>
        /// <param name="e">args</param>
        private void btnClose_Click(object sender, EventArgs e)
        {
            try
            {
                this.Close();
            }
            catch (Exception ex)
            {
                UIMessage.DisplayError(ex);
            }
        }

        #endregion Events
    }
}

我在网格的上下文菜单中使用它,例如,期望它下面的数据记录:

 /// <summary>
    /// Opens the object inspector
    /// </summary>
    /// <param name="sender">object</param>
    /// <param name="e">args</param>
    private void inspectorMenuItem_Click(object sender, EventArgs e)
    {
        try
        {
            //Get the payload
            SectionDataTreeListMenuItemPayload payload = (sender as ToolStripMenuItem).Tag as SectionDataTreeListMenuItemPayload;

            if (payload == null || payload.DataRow == null)
                return;

            using (frmInspector frmInspector = new frmInspector(payload.DataRow))
                frmInspector.ShowDialog();
        }
        catch (Exception ex)
        {
            UIMessage.DisplayError(ex);
        }
    }

您可以做的另一个小技巧是确保只有在调试模式下构建时才能使用以下代码使用&#39;编译器指令&#39;。 (如果你想要它只用于调试当然)

#if DEBUG

                //Add object inspector menu item if built in debug mode
                ToolStripMenuItem inspectorMenuItem = new ToolStripMenuItem();
                inspectorMenuItem.Text = "Inspect Object";
                inspectorMenuItem.Image = Properties.Resources.Inspect24x24;
                inspectorMenuItem.Click += inspectorMenuItem_Click;
                inspectorMenuItem.Tag = payload;
                contextMenuStrip.Items.Add(inspectorMenuItem);

#endif

答案 1 :(得分:3)

PropertyGrid

var form = new Form();

form.Controls.Add
 (
   new PropertyGrid() 
   { 
     SelectedObject = new { A = "Hi", B = new [] { 32, 40 } } 
   }
 );

form.Show();

它与调试器的工作方式相差甚远,但它可以很容易地修改,以处理您可能遇到的任何特殊情况。