Get all properties marked with a specific attribute

I have a class and properties. An attribute may be marked on some properties (it inherits my LocalizedDisplayName from DisplayNameAttribute ). This is the method to get all the properties of a class:

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

I want to add class properties to the list marked LocalizedDisplayName and display the attribute value in the list. How can i do this?

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 a string from a resource file. Thank.

+60
reflection c #
Sep 05 2018-11-11T00:
source share
1 answer

Perhaps the easiest way is to use IsDefined :

 var properties = type.GetProperties() .Where(prop => prop.IsDefined(typeof(LocalizedDisplayNameAttribute), false)); 

EDIT: To get the values โ€‹โ€‹yourself, you should use:

 var attributes = (LocalizedDisplayNameAttribute[]) prop.GetCustomAttributes(typeof(LocalizedDisplayNameAttribute), false); 
+95
Sep 05 2018-11-11T00:
source share



All Articles