0

I'm trying to get the runtime value of a property defined in a class that's modified at runtime without any luck so far in C# .NET 4.0. Any guideline about how to do that would be appreciated.

Lets say I have a class named Test33 which has a property named DisplayTwoDecimals on which I applied Browsable(false). Later in the code I will update the value of Browsable to true. I'd like to inspect what the value of Browsable attribute is at runtime and I expect to get the value true.

Here is the Test33 class:

public class Test33
{
    [Browsable(false)]
    public bool DisplayTwoDecimals
    {
            get; set;
    }

    public void ChangeBrowsableAttribute(bool value, string property)
    {
        PropertyDescriptor pDesc = TypeDescriptor.GetProperties(GetType())[property];
        BrowsableAttribute attrib = (BrowsableAttribute)pDesc.Attributes[typeof(BrowsableAttribute)];
        FieldInfo isBrowsable = attrib.GetType().GetField("browsable", BindingFlags.NonPublic | BindingFlags.Instance);
        isBrowsable.SetValue(attrib, value);
    }

    public static bool GetBrowsable(PropertyInfo property)
    {
        var atts = property.GetCustomAttributes(typeof(BrowsableAttribute), true);
        if (atts.Length == 0)
                return true;

        return (atts[0] as BrowsableAttribute).Browsable;
    }
}

Following code fragment shows the value of Browsable attribute at runtime in a rich text box:

public Form1()
{
    InitializeComponent();

    Test33 t = new Test33();
    t.ChangeBrowsableAttribute(true, "DisplayTwoDecimals");

    List<PropertyInfo> pis = t.GetType().GetProperties().ToList();
    richTextBox1.AppendText(Test33.GetBrowsable(pis[0]).ToString() + "\n");
}

Despite the face that I've changed the Browsable attribute to true, the rich text box is showing False.

EDIT

This is not same as setting some value to a attribute at runtime.

Donotalo
  • 12,748
  • 25
  • 83
  • 121
  • Attributes are metadata. The are not meant to be changed. I dont even know whether the are singletons or recreated regularly. – leppie Jul 17 '15 at 05:44
  • @leppie: We're in a situation that on the UI in a property grid some items are shown/hidden at runtime. We need to generate a CSV file for compatibility with another application. The CSV file should contain all visible properties and values of the property grid. We are using BrowsableAttribute to show/hide properties. So we need to know their value at runtime. – Donotalo Jul 17 '15 at 05:53
  • The linked question has the proper solution. http://stackoverflow.com/questions/1440108/setting-value-of-browsableattribute-at-runtime – leppie Jul 17 '15 at 05:59
  • @leppie Related links are down btw (Part 1, Part 2). (http://stackoverflow.com/questions/1440108/setting-value-of-browsableattribute-at-runtime) – Thus Spoke Nomad Jul 17 '15 at 06:10
  • @MonoLightTech: I see that too. Will reopen till I can find proper one answer. `ICustomTypeDescriptor` is the correct way to go though. – leppie Jul 17 '15 at 06:18
  • I think I haven't explained enough what I look for. I've updated the post. I need to get the value of an attribute of a property after the attribute value has changed at runtime. So `Browsable(false)` was declared for a property and later at runtime it was set to `true`. I wanted a way to get this updated value: `true`. – Donotalo Jul 17 '15 at 10:31
  • 1
    @leppie: I indeed got the solution from here: http://stackoverflow.com/a/3912001/68304 – Donotalo Jul 17 '15 at 10:35

0 Answers0