在运行时向变量添加显示属性

本文关键字:显示 属性 添加 变量 运行时 | 更新日期: 2023-09-27 18:20:48

我有一个类

class demo
{
    public int var1 {set; get;}
}

我想在运行时添加该变量的显示属性,而不是这样做,

class Demo
{
    [Display (Name="Any Name", GroupName= "My Group 1")]
    public int var1 {set; get;}
}

有任何可能的方法来更改或分配任何其他类的属性吗?

在运行时向变量添加显示属性

非常简单的例子,可以构建它,在PropertyGrid中您会看到一些东西。您需要阅读有关ICustomTypeDescriptor和PropertyDescriptor的信息。

表单加载:

propertyGrid1.SelectedObject = new MyType(new[] { "Property1", "Property2" });

类型:

public class MyType : ICustomTypeDescriptor
{
    private string[] _properties;
    public MyType(string[] properties)
    {
        _properties = properties;
    }
    public AttributeCollection GetAttributes()
    {
        return null;
    }
    public string GetClassName()
    {
        return nameof(MyType);
    }
    public string GetComponentName()
    {
        throw new NotImplementedException();
    }
    public TypeConverter GetConverter()
    {
        return null;
    }
    public EventDescriptor GetDefaultEvent()
    {
        throw new NotImplementedException();
    }
    public PropertyDescriptor GetDefaultProperty()
    {
        return null;
    }
    public object GetEditor(Type editorBaseType)
    {
        return null;
    }
    public EventDescriptorCollection GetEvents()
    {
        throw new NotImplementedException();
    }
    public EventDescriptorCollection GetEvents(Attribute[] attributes)
    {
        throw new NotImplementedException();
    }
    public PropertyDescriptorCollection GetProperties()
    {
        return GetProperties(null);
    }
    public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
    {
        var props = new PropertyDescriptor[_properties.Length];
        for (int i = 0; i < _properties.Length; i++)
            props[i] = new CustomPropertyDescriptor(_properties[i],
                new Attribute[]
                {
                    new DisplayNameAttribute(@"Displ Value " + i),
                    new CategoryAttribute("Category" + i%2)
                });
        return new PropertyDescriptorCollection(props);
    }
    public object GetPropertyOwner(PropertyDescriptor pd)
    {
        return this;
    }
}
public class CustomPropertyDescriptor : PropertyDescriptor
{
    public CustomPropertyDescriptor(string name, Attribute[] attrs) : base(name, attrs)
    {
    }
    public override bool CanResetValue(object component)
    {
        return true;
    }
    public override object GetValue(object component)
    {
        return "1";
    }
    public override void ResetValue(object component)
    {
        throw new NotImplementedException();
    }
    public override void SetValue(object component, object value)
    {
        throw new NotImplementedException();
    }
    public override bool ShouldSerializeValue(object component)
    {
        return false;
    }
    public override Type ComponentType { get; }
    public override bool IsReadOnly { get { return false; } }
    public override Type PropertyType { get { return typeof (string); } }
}