获取标记为某个属性的所有属性

我有类和属性在那里。一些属性可以标记属性(它是我的 LocalizedDisplayNameDisplayNameAttribute继承)。 这是获取 class 的所有属性的方法:

private void FillAttribute()
{
Type type = typeof (NormDoc);
PropertyInfo[] propertyInfos = type.GetProperties();
foreach (var propertyInfo in propertyInfos)
{
...
}
}

我想在列表框中添加类的属性,这个列表框标记为 LocalizedDisplayName,并在列表框中显示属性值。我怎么能这么做?

剪辑
这是 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);
}
}

我想从资源文件中获取字符串。 谢谢。

44536 次浏览

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);