46103

How can I retrieve all custom attributes on a class

Question:

I'm trying to retrieve a list all attributes applied to my class.

I can see the Attribute.GetCustomAttributes() series of methods, but I can only see methods to retereive all attributes for assemblies, modules, members, and properties - nothing for a class. The versions of the method that take a Type as an argument are not appropriate because they only return attributes of that type, whereas I want to iterate through all the attributes in order.

Why am I doing this? I'm extending my application, and to add some simple UI mods I want to load the user's code, and use the attributes to define the controls, eg...

[MyTextBox("Address1:")] [MyButton("QueryStatus")] [MyButton("QuerySpeed/Pos")] public class QueryStatus {

I've loaded the assembly and can access the class Type. Is there another place I can find all of my custom attributes?

Answer1:

Just use <a href="http://msdn.microsoft.com/en-us/library/system.type.getcustomattributes.aspx" rel="nofollow">Type.GetCustomAttributes()</a>.

Type type = ...; foreach (var attribute in type.GetCustomAttributes(inherit: false)) { // Whatever you need to do }

Recommend

  • Extending the User Interface in Outlook 2010 to add to the Right Click menu
  • Is PascalScript compatible with Delphi XE5 to create Android/iOS/Windows applications?
  • Is it mandatory to have a doGet or doPost method?
  • Avoid Inheriting Super Class Tests in ScalaTest
  • Retaining data after updating application
  • How to handle elastic beanstalk deployment so it uploads only changed files
  • Filter strings with regex before casting to numeric
  • gspread or such: help me get cell coordinates (not value)
  • How to define custom class, title, and target in Link Browser for content elements and the new rte_c
  • How do I exclude a dependency in provided scope when running in Maven test scope?
  • d3 v4 drag and drop with TypeScript
  • Why does access(2) check for real and not effective UID?
  • Q promise. Difference between .when and .then
  • Illegal mix of collations for operation for date/time comparison
  • C# - Serializing and deserializing static member
  • Sending data from AppleScript to FileMaker records
  • Trying to switch camera back to front but getting exception
  • Warning: Can't call setState (or forceUpdate) on an unmounted component
  • R: gsub and capture
  • Arrays break string types in Julia
  • How to format a variable of double type
  • VB.net deserialize, JSON Conversion from type 'Dictionary(Of String,Object)' to type '
  • How to include full .NET prerequisite for Wix Burn installer
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • Free memory of cv::Mat loaded using FileStorage API
  • C# - Getting references of reference
  • JTable with a ScrollPane misbehaving
  • Angular 2 constructor injection vs direct access
  • How do I configure my settings file to work with unit tests?
  • Java static initializers and reflection
  • Android Google Maps API OnLocationChanged only called once
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Is it possible to post an object from jquery to bottle.py?
  • Django query for large number of relationships
  • Programmatically clearing map cache
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • Binding checkboxes to object values in AngularJs
  • How can I use `wmic` in a Windows PE script?
  • UserPrincipal.Current returns apppool on IIS
  • How to push additional view controllers onto NavigationController but keep the TabBar?