C# 类别属性返回”;杂项“;对于命名为“的类别”;“违约”;
我有一个名为C# 类别属性返回”;杂项“;对于命名为“的类别”;“违约”;,c#,.net,winforms,attributes,system.componentmodel,C#,.net,Winforms,Attributes,System.componentmodel,我有一个名为CommonKeys.cs的类,它包含一个属性,如下所示 public class Test { private SolidBrush _backgroundbrush; [CategoryAttribute("Default")] public SolidBrush BackgroundBrush { get { return this._backgroundbrush; } se
CommonKeys.cs
的类,它包含一个属性,如下所示
public class Test
{
private SolidBrush _backgroundbrush;
[CategoryAttribute("Default")]
public SolidBrush BackgroundBrush
{
get
{
return this._backgroundbrush;
}
set
{
this._backgroundbrush = value;
}
}
}
当我使用下面的代码访问上述属性及其类别时,它返回“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”时,它会返回准确的类别名称
我的问题是,当“Default”设置为category时,为什么会返回“Misc”
问候,
Amal Raj这是因为类的实现。对于某些值,它从.net framework的字符串资源中获取类别名称。其中一个值是
默认值
,其定义如下:
PropertyCategoryDefault = Misc
您还将收到另一个有关Config
、DragDrop
和WindowStyle
的文本:
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
}
你问了18个问题,只接受了一个答案。请回顾您之前的问题和当前的问题,并将正确答案标记为已接受,并投票选出有用的答案,包括已接受的答案:)