代码之家  ›  专栏  ›  技术社区  ›  Nick Spreitzer

如何从SettingsProperty检索Description属性?

  •  4
  • Nick Spreitzer  · 技术社区  · 14 年前

    对于应用程序设置中的每个项目,我都将文本添加到其 Description Property

    任何关于如何正确检索value Description属性的见解都将非常感激。谢谢。:)

    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...
            }
        }
    
    3 回复  |  直到 14 年前
        1
  •  0
  •   Jeffrey L Whitledge    14 年前

    问题是Attributes[]索引器中的键不接受字符串。

    试试这个:

        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... 
            }
        }
    
        2
  •  4
  •   Hans Passant    14 年前

    此代码显示了具有描述的所有设置:

    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
  •  0
  •   Igby Largeman    14 年前

    HashTable

        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
                    }
                }
            }
    
        }