Craig Gidney
Craig Gidney

Reputation: 18306

How do I give a method as an attribute argument?

I want to apply an attribute which accepts a delegate as an argument, but I can't find the syntax to do it.

For example, to pass a class, you have to use typeof:

    [SomeAttribute(typeof(SomeClass))]

What is the syntax for a delegate (I am trying to pass a static method)?

    [SomeAttribute(??? SomeStaticMethod ???]

Upvotes: 8

Views: 2114

Answers (1)

Quintin Robinson
Quintin Robinson

Reputation: 82375

This is beyond the capacity of the Meta Data.

You could pass the method as a string and also specify the class if you just want to call a static method..

[SomeAttribute(typeof(SomeClass), @"SomeStaticMethod")]

Naturally you would have to invoke it through reflection, but since you are looking up the custom attributes anyway this probably isn't a big deviation.

Upvotes: 8

Related Questions