简体   繁体   English

c# winform 在运行时检查对象

[英]c# winform to inspect an object during runtime

I want to display the public Property/Values of an arbitrary object during runtime in a GUI.我想在运行时在 GUI 中显示任意对象的公共属性/值。

Is there a winform which allows the user to view the content of any object like in debug mode?是否有一个 winform 允许用户像在调试模式下一样查看任何对象的内容? The object will hold many Dictionaries > and it would be nice to be able to expand and view the contents of those lists during runtime.该对象将保存许多 Dictionaries > 并且能够在运行时扩展和查看这些列表的内容会很好。

If not available, is there someway to achieve something similar?如果不可用,是否有办法实现类似的目标?

Thanks谢谢

All you need to do is create a form with a PropertyGrid on it.您需要做的就是创建一个带有 PropertyGrid 的表单。 Then set the selected object.然后设置选定的对象。

在此处输入图片说明

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

I use this on a context menu in a grid for example to expect the data record below it:我在网格中的上下文菜单上使用它,例如期待它下面的数据记录:

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

another little trick you can do is to ensure the form is only available when built in debug mode by using the below code using a 'compiler directive'.您可以做的另一个小技巧是通过使用以下代码使用“编译器指令”来确保表单仅在调试模式下构建时可用。 (if you want it only used for debugging of course) (当然,如果您希望它仅用于调试)

#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

There's PropertyGrid :PropertyGrid

var form = new Form();

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

form.Show();

It's quite far from how the debugger one works, but it can be quite easily modified to handle any special cases you might have.它与调试器的工作方式相去甚远,但可以很容易地对其进行修改以处理您可能遇到的任何特殊情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM