How can I retrieve all custom attributes on a clas

2019-08-31 02:37发布

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 retrieve 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?

1条回答
劫难
2楼-- · 2019-08-31 02:58

Just use Type.GetCustomAttributes().

Type type = ...;
foreach (var attribute in type.GetCustomAttributes(inherit: false))
{
    // Whatever you need to do
}
查看更多
登录 后发表回答