1

I have a class

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

I want to add Display attributes on that variable on runtime instead of doing this,

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

Any possible way to change or Assign those attributes from any other class?

Il Vic
  • 5,576
  • 4
  • 26
  • 37
  • Use search... It's impossible, but you can use TypeDescriptor, for example. – Spawn Nov 06 '15 at 14:35
  • Not without constructing a new type or doing some sort of IL weaving. Attributes are defined at compile time and compiled into the assembly. – Ron Beyer Nov 06 '15 at 14:36
  • @Spawn Any example how to use TypeDescriptor ? I have made TypeDescriptor object but have no idea how to bind it with any variable. – Sayed Hussain Mehdi Nov 06 '15 at 14:47
  • 3
    At the first, why you need it? Do you want to use it with DataGrid, or PropertyGrid, or what? – Spawn Nov 06 '15 at 14:57
  • @Spawn using with PropertyGrid, I am basically reading properties to be loaded from file and creating list of string and want to associate them group and names. – Sayed Hussain Mehdi Nov 06 '15 at 15:43

1 Answers1

1

Very simple example, it can be build and in PropertyGrid you will see something. You need read about ICustomTypeDescriptor and PropertyDescriptor.

On Form Load:

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

Types:

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); } }
}
Spawn
  • 935
  • 1
  • 13
  • 32