.net - 使用 PropertyGrid 设置 PerformanceCounter

标签 .net vb.net c#-4.0 propertygrid performancecounter

我想通过使用 PropertyGrid 设置参数来动态创建一个 PerformanceCounter。如果我将 PropertyGrid.SelectedObject 设置为 PerformanceCounter,它不会提供与 IDE 中相同的选项。 enter image description here

我该怎么做?有可能吗?

最佳答案

主要问题是某些 PerformanceCounter 属性(例如 CategoryName)标有 ReadOnly属性。当属性网格在属性上看到 ReadOnly 属性时,您无法对其进行编辑,因此您无法使用包含类别名称列表的下拉菜单。

我不知道设计师用了什么魔法来克服这个问题,但这是我的解决方案。我们添加自定义 TypeDescriptionProvider到已编辑的 PerformanceCounter 实例,并提供一个自定义类型描述符以消除 ReadOnly 限制。

这是您可以如何使用它(使用 PropertyGrid 类的 propertyGrid1 实例):

PerformanceCounter counter = new PerformanceCounter();

// use a custom type description provider for this counter
TypeDescriptor.AddProvider(new PerformanceCounterTypeProvider(), counter);

// filter unwanted properties
propertyGrid1.BrowsableAttributes = new AttributeCollection(DesignerSerializationVisibilityAttribute.Visible);

// select it in the property grid
propertyGrid1.SelectedObject = counter;

这是使用的实用程序代码:

public class PerformanceCounterTypeProvider : TypeDescriptionProvider
{
    private static PropertyDescriptor[] _properties;

    static PerformanceCounterTypeProvider()
    {
        List < PropertyDescriptor> properties = new List<PropertyDescriptor>();
        foreach (PropertyDescriptor pd in TypeDescriptor.GetProperties(typeof(PerformanceCounter)))
        {
            PerformanceCounterPropertyDescriptor clone = new PerformanceCounterPropertyDescriptor(pd);
            properties.Add(clone);
        }
        _properties = properties.ToArray();
    }

    public override ICustomTypeDescriptor GetTypeDescriptor(Type objectType, object instance)
    {
        return new PerformanceCounterTypeDescriptor();
    }

    private class PerformanceCounterTypeDescriptor : CustomTypeDescriptor
    {
        public override PropertyDescriptorCollection GetProperties(Attribute[] attributes)
        {
            return new PropertyDescriptorCollection(PerformanceCounterTypeProvider._properties.ToArray());
        }
    }

    private class PerformanceCounterPropertyDescriptor : PropertyDescriptor
    {
        private PropertyDescriptor _desc;

        public PerformanceCounterPropertyDescriptor(PropertyDescriptor desc)
            : base(desc, new List<Attribute>(desc.Attributes.OfType<Attribute>()).ToArray())
        {
            _desc = desc;
        }

        public override void SetValue(object component, object value)
        {
            // we can't use _desc.SetValue because the underlying property descriptor checks it's read only
            ComponentType.GetProperty(Name).SetValue(component, value, null);
        }

        public override bool IsReadOnly
        {
            get { return false; }
        }

        public override bool CanResetValue(object component)
        {
            return _desc.CanResetValue(component);
        }

        public override Type ComponentType
        {
            get { return _desc.ComponentType; }
        }

        public override object GetValue(object component)
        {
            return _desc.GetValue(component);
        }

        public override Type PropertyType
        {
            get { return _desc.PropertyType; }
        }

        public override void ResetValue(object component)
        {
            _desc.ResetValue(component);
        }

        public override bool ShouldSerializeValue(object component)
        {
            return _desc.ShouldSerializeValue(component);
        }
    }
}

关于.net - 使用 PropertyGrid 设置 PerformanceCounter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17156438/

相关文章:

c# - 如何使用异步任务获取数据并在 View 中显示?

c# - 在 C#(.Net Compact Framework)中完全加载我的表单时的通知?

c# - ASP.NET Core 托管的 Blazor 模板中的授权问题

javascript - 获取Controller的返回值给javascript

VB.NET - 在没有外部 DLL 的情况下集成 Win7 任务栏进度?

.net - 从 ms-access 客户端界面切换到可执行文件

vb.net - 在 VB.NET 中切换 TopMost

c# - 通过C#读取SQL Server中存储过程设置的值

c# - 是否可以解决这种装配不匹配的情况?还是这个问题无法解决?

时间:2019-01-17 标签:c#evalfunction