gpt4 book ai didi

.net - 使用 PropertyGrid 设置 PerformanceCounter

转载 作者:行者123 更新时间:2023-12-01 15:43:16 25 4
gpt4 key购买 nike

我想通过使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com