获取标记特定属性的所有属性 [英] Get all properties which marked certain attribute
问题描述
我在那里有类和属性.某些属性可以标记为属性(它是我的 LocalizedDisplayName
继承自 DisplayNameAttribute
).这是获取类的所有属性的方法:
I have class and properties in there. Some properties can be marked attribute (it's my LocalizedDisplayName
inherits from DisplayNameAttribute
).
This is method for get all properties of class:
private void FillAttribute()
{
Type type = typeof (NormDoc);
PropertyInfo[] propertyInfos = type.GetProperties();
foreach (var propertyInfo in propertyInfos)
{
...
}
}
我想在标记为 LocalizedDisplayName
的列表框中添加类的属性,并在列表框中显示属性的值.我该怎么做?
I want to add properties of class in the listbox which marked LocalizedDisplayName
and display value of attribute in the listbox. How can I do this?
编辑
这是 LocalizedDisplayNameAttribute:
EDIT
This is LocalizedDisplayNameAttribute:
public class LocalizedDisplayNameAttribute : DisplayNameAttribute
{
public LocalizedDisplayNameAttribute(string resourceId)
: base(GetMessageFromResource(resourceId))
{ }
private static string GetMessageFromResource(string resourceId)
{
var test =Thread.CurrentThread.CurrentCulture;
ResourceManager manager = new ResourceManager("EArchive.Data.Resources.DataResource", Assembly.GetExecutingAssembly());
return manager.GetString(resourceId);
}
}
我想从资源文件中获取字符串.谢谢.
I want to get string from resource file. Thanks.
推荐答案
使用 IsDefined
:
var properties = type.GetProperties()
.Where(prop => prop.IsDefined(typeof(LocalizedDisplayNameAttribute), false));
要获取值本身,您可以使用:
To get the values themselves, you'd use:
var attributes = (LocalizedDisplayNameAttribute[])
prop.GetCustomAttributes(typeof(LocalizedDisplayNameAttribute), false);
这篇关于获取标记特定属性的所有属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!