views:

130

answers:

3

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...
        }
    }
+1  A: 

This code showed all settings that have a description:

using System;
using System.Configuration;
using System.Reflection;

namespace ConsoleApplication1 {
  class Program {
    static void Main(string[] args) {
      var all = typeof(Properties.Settings).GetProperties();
      foreach (var prop in all) {
        var attr = (SettingsDescriptionAttribute[])prop.GetCustomAttributes(typeof(SettingsDescriptionAttribute), false);
        if (attr.Length > 0) 
          Console.WriteLine("{0}: {1}", prop.Name, attr[0].Description);
      }
      Console.ReadLine();
    }
  }
}
Hans Passant
Excellent. Thank you very much for that, I really appreciate it. :)
Nick
A: 

You're trying to get a value from a HashTable by its key, but you're using the wrong key. (I guess the hashtable uses the hashvalue of the attribute itself as the key)

This change to your code demonstrates:

    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)
            {
                if (property.Attributes[theKey].GetType().Name == "SettingsDescriptionAttribute")
                {
                    SettingsDescriptionAttribute sda = property.Attributes[theKey] as SettingsDescriptionAttribute;
                    System.Diagnostics.Debug.Print(sda.Description); // prints the description
                }
            }
        }

    }
Charles
Also very helpful. Thank you! :)
Nick
Sorry, I made a mistake in my explanation - fixed now.
Charles
No worries, I appreciate your help. :D
Nick
A: 

The problem is that the key into the Attributes[] indexer does not take a string.

Try this:

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

        IEnumerator enumerator = MyAppProperties.GetEnumerator();
        object settingsDescriptionAttribute = null;

        // 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());
                if (theKey.ToString() == "System.Configuration.SettingsDescriptionAttribute")
                    settingsDescriptionAttribute = theKey;
            }
        }

        enumerator.Reset();

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

            string propertyValue = property.DefaultValue.ToString();

            // This fails: Null Reference  
            string propertyDescription =
                property.Attributes[settingsDescriptionAttribute].ToString();

            // Do stuff with strings... 
        }
    }
Jeffrey L Whitledge
Very helpful. Thank you! :)
Nick