Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get action name as string for Attribute-parameter

I have some Action Attributes that allow parameters. This is how it looks like:

[DeleteActionCache(Action="GetTerms")]
public ActionResult AddTerm(string term){ }

public ActionResult GetTerms() {}

Now I want to get rid of the magic string "GetTerms" in my Attribute. So I would prefer something like: (Pseudocode, not working)

[DeleteActionCache(Action=this.GetTerms().GetType().Name)]

Having an additional Property inside my attribute-class and doing "Method2String"-Conversions inside my that class would be ok with me if this is needed to achieve what I want.

Info: I am not looking for a way the get the current method name (MethodBase.GetCurrentMethod)

like image 263
Ole Albers Avatar asked Mar 01 '17 11:03

Ole Albers


1 Answers

nameof could help here:

[DeleteActionCache(Action=nameof(GetTerms))]

Depending on the use of the property, you might be better of handling this at the place the attribute is read from the member, but in this case it seems hard since there isn't a fixed relationship between the action and the property.

like image 79
Patrick Hofman Avatar answered Sep 20 '22 04:09

Patrick Hofman