Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ???]
like image 779
Craig Gidney Avatar asked Oct 24 '09 15:10

Craig Gidney


1 Answers

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.

like image 71
Quintin Robinson Avatar answered Sep 19 '22 18:09

Quintin Robinson