0

Is there a way to find all the properties of a given type without the BrowsableAttribute explicitly set to Yes in .Net?

I've tried the following code without success (all the properties which are browsable by default are also returned):

PropertyDescriptorCollection browsableProperties = TypeDescriptor.GetProperties(type, new Attribute[] { BrowsableAttribute.Yes });
Gyum Fox
  • 3,287
  • 2
  • 41
  • 71
  • This is kind of broad.. What answer are you looking for? Have you looked at PropertyInfo, Reflection, anything? Short answer is probably "yes", is that what you want to know? – Patrick Aug 05 '14 at 10:21
  • Any answer welcomed :) I'm updating my question with what I tried if that can help – Gyum Fox Aug 05 '14 at 10:23
  • Well yeah, defining what expected outcome you want from a given class for instance would help immensely for adding constraints to the problem, and adding information for future readers. – Patrick Aug 05 '14 at 10:24

1 Answers1

1

A bit of reflection and linq will help here.

var result =  type
    .GetProperties()
    .Where(x =>
            x.GetCustomAttribute<BrowsableAttribute>() == null ||
            !x.GetCustomAttribute<BrowsableAttribute>().Browsable)
    .ToList();

You can introduce a local variable to avoid calling GetCustomAttribute method twice.

If you're in .Net framework version less than 4.5 you can write your own GetCustomAttribute extension method like this

public static T GetCustomAttribute<T>(this MemberInfo element) where T: Attribute
{
    return (T) element.GetCustomAttribute(typeof(T));
}
Sriram Sakthivel
  • 72,067
  • 7
  • 111
  • 189
  • Thanks, there are a few errors (it's GetCustomAttributes, not GetCustomAttribute, and it's missing a cast), but I got the idea and it does the job! – Gyum Fox Aug 05 '14 at 10:46
  • @GyumFox There is also a version of [GetCustomAttribute](http://msdn.microsoft.com/en-us/library/hh194292%28v=vs.110%29.aspx) – Sriram Sakthivel Aug 05 '14 at 10:54
  • [PropertyInfo](http://msdn.microsoft.com/en-us/library/system.reflection.propertyinfo(v=vs.110).aspx) does not have a GetCustomAttribute method though, which is what you are working with – Gyum Fox Aug 05 '14 at 11:55
  • Ah my bad, I didn't see the extension method – Gyum Fox Aug 05 '14 at 11:57
  • Looks like GetCustomAttribute is only available starting form .NET 4.5 – Gyum Fox Aug 05 '14 at 12:07
  • @SriramSakthivel If you see the edit history you will get that I had improved your answer not vandalized. If you see my last edit I had added `using` in the snippet which is necessary if using Extension method. My edit was necessary so that anyone visiting in future would get the answer as whole without going thru comments where you have provided the final part of the answer to the OP i.e. add the `using System.Reflection` to exactly use provided code. – Harsh Baid Aug 05 '14 at 12:12
  • @HarshBaid What about first two edits? Wrong edit, then rolled back? Why? using directive is trivial one. and I've not added my code in any method which is invalid in c# so is that a problem? – Sriram Sakthivel Aug 05 '14 at 12:15
  • @SriramSakthivel I'm using .Net 4.0 Thanks though for improving the answer and making clear what is the supported version. – Gyum Fox Aug 05 '14 at 12:16
  • @GyumFox I've updated my answer, you can write your own extension method easily :) – Sriram Sakthivel Aug 05 '14 at 12:16
  • I made initial edit based on very first comment by @GyumFox then I saw your comment that your code is correct and you have used extension method so I rollback and added the `using` block to complete the answer for other for whom adding the extension method is not so obvious (coz they may not have Resharper which has intellisense for extension method). Try once by creating new project and this block then try `ctrl`+`.` it won't show anything and generally we will thing that its bad code, until you manually go and add the minimum required `using` on top of your class. – Harsh Baid Aug 05 '14 at 12:23