名为 "Default" 的类别的类别属性 returns "Misc"

CategoryAttribute returns "Misc" for category named as "Default"

我有一个名为 CommonKeys.cs 的 class,它包含一个 属性,如下所示,

public class Test
{
   private SolidBrush _backgroundbrush;
   [CategoryAttribute("Default")]
   public SolidBrush BackgroundBrush
   {
       get
       {
           return this._backgroundbrush;
       }
       set
       {
           this._backgroundbrush = value;
       }
   }
}

当我使用以下代码访问上面的 属性 及其类别时,它 return "Misc" 作为类别而不是原始类别 "Default" .

public static void GetCategoryName()
{
   PropertyInfo[] Props = typeof(Test).GetProperties(BindingFlags.Public | BindingFlags.Instance);
   foreach (PropertyInfo prop in Props)
   {
       var attributes = prop.GetCustomAttributes(false);
       string categoryName = String.Empty;
       foreach (var attr in attributes)
       {
           if (attr is CategoryAttribute)
           {
               categoryName = (attr as CategoryAttribute).Category;
           }
       }
   }
}

但是当我更改 "Default" 以外的类别名称时,它 return 确切的类别名称。

我的问题是,当 "Default" 被设置为类别时,为什么 "Misc" 被 return 编辑。

此致,

阿玛拉吉

这是因为 CategoryAttribute class 的实施。对于某些值,它从 .net 框架的字符串资源中获取类别名称。其中一个值是 Default,它是这样定义的:

PropertyCategoryDefault = Misc

您还将收到 ConfigDragDropWindowStyle 的另一条短信:

PropertyCategoryConfig = Configurations
PropertyCategoryDragDrop = Drag Drop
PropertyCategoryWindowStyle = Window Style

相关实现如下:

public string Category {
    get {
        if (!localized) {
            localized = true;
            string localizedValue = GetLocalizedString(categoryValue);
            if (localizedValue != null) {
                categoryValue = localizedValue;
            }
        }
        return categoryValue;
    }
}
protected virtual string GetLocalizedString(string value) {
#if !SILVERLIGHT
    return (string)SR.GetObject("PropertyCategory" + value);
#else
    bool usedFallback;
    string localizedString = SR.GetString("PropertyCategory" + value, out usedFallback);
    if (usedFallback) {
        return null;
    }
    return localizedString;
#endif
}