c# winform 在运行时检查对象

标签 c# .net winforms

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

是否有一个 winform 允许用户像在 Debug模式下一样查看任何对象的内容?该对象将包含许多词典 > 并且能够在运行时扩展和查看这些列表的内容会很好。

如果不可用,是否有办法实现类似的目标?

谢谢

最佳答案

您需要做的就是创建一个带有 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);
        }
    }

您可以做的另一个小技巧是确保表单仅在 Debug模式下构建时可用,方法是使用以下代码和“编译器指令”。 (当然,如果你希望它只用于调试)

#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

关于c# winform 在运行时检查对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30910353/

相关文章:

c# - 具有相同标识的多个程序集。 .NETFramework 门面

c# - 有没有理由我不应该从 C# 开始

powershell - 如何退出在系统托盘中运行的 powershell 脚本?

c# - 韩国操作系统上的控件大小增加

c# - Asp.Net Core 2 验证不记名 token

c# - .NET 和带 DLLS 的配置文件

c# - 使用 DevExpress 或 Infragistics 将 HTML 或 PDF 转换为 RTF/DOC 或将 HTML/PDF 转换为图像

c# - 在 MVC Controller 中将 Javascript 时间字符串转换为 DateTime 或 TimeSpan

c# - 线程池和并行。对于组合 - 是否可能

c# - .Net 中的多语言 Winform - 意见和建议