How to retrieve the Description property from SettingsProperty?

Posted by BadNinja on Stack Overflow See other posts from Stack Overflow or by BadNinja
Published on 2010-04-05T20:44:19Z Indexed on 2010/04/05 21:33 UTC
Read the original article Hit count: 295

Filed under:
|
|

For each item in my application's settings, I've added text to its Description Property which I want to retrieve at runtime. I'm sure I'm missing some basic logical nuance here, but everything I've tried has failed. Clearly, my understanding of what value needs to be passed to the Attributes property of the SettingsProperty class is wrong. I'm further confused by the fact that when I iterate through all they keys returned by SettingsProperty.Attributes.Keys, I can see "System.Configuration.SettingsDescriptionAttribute", but when I pass that string in as the key to the Attributes property, null is returned.

Any insight into how to properly retrieve the value Description Property would be very much appreciated. Thanks. :)

public void MyMethod()
    {
        SettingsPropertyCollection MyAppProperties = 
            Properties.Settings.Default.Properties;

        IEnumerator enumerator = MyAppProperties.GetEnumerator();

        // Iterate through all the keys to see what we have....
        while (enumerator.MoveNext())
        {
            SettingsProperty property = (SettingsProperty)enumerator.Current;
            ICollection myKeys = property.Attributes.Keys;

            foreach (object theKey in myKeys)
                System.Diagnostics.Debug.Print(theKey.ToString());
                // One of the keys returned is: 
                   System.Configuration.SettingsDescriptionAttribute
        }

        enumerator.Reset();

        while (enumerator.MoveNext())
        {
            SettingsProperty property = (SettingsProperty)enumerator.Current;

            string propertyValue = property.DefaultValue.ToString();

            // This fails: Null Reference 
            string propertyDescription = 
                property.Attributes["System.Configuration.SettingsDescriptionAttribute"].ToString();

            // Do stuff with strings...
        }
    }

© Stack Overflow or respective owner

Related posts about c#

Related posts about settings