获取这标志着某些属性的所有属性 [英] 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
:
It's probably easiest to use 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屋!