对于我的应用程序设置中的每个项目,我在其Description Property
中添加了文本,我想在运行时检索它。我确信我在这里缺少一些基本的逻辑细微差别,但我尝试过的一切都失败了。显然,我对需要传递给SettingsProperty类的Attributes属性的值的理解是错误的。我进一步感到困惑的是,当我遍历SettingsProperty.Attributes.Keys返回的所有键时,我可以看到“System.Configuration.SettingsDescriptionAttribute”,但是当我将该字符串作为键传递给Attributes属性时,返回null。
如何正确检索值描述属性的任何见解将非常感激。谢谢。 :)
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 :(得分:3)
此代码显示了所有具有说明的设置:
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();
}
}
}
答案 1 :(得分:0)
您尝试通过其密钥从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
}
}
}
}
答案 2 :(得分:0)
问题是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...
}
}