2010-04-05 3 views
4

내 응용 프로그램 설정의 각 항목에 대해 런타임에 검색 할 Description Property에 텍스트를 추가했습니다. 나는 여기에 기본적인 논리적 인 뉘앙스를 놓치고 있다고 확신하지만, 내가 시도한 모든 것은 실패했다. 분명히, 어떤 값을 SettingsProperty 클래스의 Attributes 속성에 전달해야하는지에 대한 나의 이해가 잘못되었습니다. 나는 그 모든 것들을 반복 할 때 SettingsProperty.Attributes.Keys에 의해 리턴 된 키를 통해 "System.Configuration.SettingsDescriptionAttribute"를 볼 수 있지만, 그 캐릭터 라인을 Attributes 속성의 키로 전달하면 혼란 스럽다. null가 리턴됩니다.SettingsProperty에서 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... 
     } 
    } 
+0

혹시'foreach' 들어 본 적이 ??? – Will

+0

아니요, 열거자를 고풍스럽게 사용하면 나에게 동맥류가 생겼습니다. 나는 당신의 질문에 답할 것이지만 나는 그것이 최선의 방법이라고 생각하지 않는다 ... – Will

+0

한때, 참으로, 나는 또한 멍청한 존재였다. 아아, 그 고통이 다시 ... – Will

답변

0

문제는 속성에 키는 [] 인덱서는을하지 않는 것이 :

코드에 이러한 변화가 보여 (I는 해시 테이블 키로 속성 자체의 hashvalue를 사용 추측) 끈.

이 시도 :

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

매우 도움이됩니다. 고맙습니다! :) –

3

이 코드는 설명이 모든 설정을 보여 주었다 : 당신은 그것의 키에 의한 HashTable에서 값을 얻기 위해 노력하고

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

우수. 정말 고마워, 정말 고마워. :) –

0

을,하지만 당신은 잘못된 키를 사용하고 있습니다.

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

또한 매우 도움이됩니다. 고맙습니다! :) –

+0

죄송합니다, 제 설명에서 실수를했습니다 - 지금 고쳐 썼습니다. –

+0

걱정하지 마시고, 협조 해 주셔서 감사합니다. : D –

관련 문제