How to get Description property from SettingsProperty?

For each element in the settings of my application, I added text to my Description Property , which I want to get at runtime. I am sure that there is no basic logical nuance here, but everything I tried failed. Clearly, my understanding of what value needs to be passed to the Attributes attribute of the SettingsProperty class is incorrect. I am even more confused by the fact that when I repeat all the keys returned by SettingsProperty.Attributes.Keys, I can see "System.Configuration.SettingsDescriptionAttribute", but when I pass this string as a key to the attribute property, null returns.

Any understanding of how to get the value of Property Description would be greatly appreciated. Thank you. :)

 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... } } 
+4
source share
3 answers

The problem is that the key in the attribute indexer [] does not accept the string.

Try the following:

  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... } } 
0
source

In this code, all parameters that have a description were shown:

 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(); } } } 
+3
source

You are trying to get a value from a HashTable by its key, but you are using the wrong key. (I think the hash table uses the hashvalue of the attribute itself as a 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 } } } } 
0
source

Source: https://habr.com/ru/post/1306054/


All Articles