Get action name as string for Attribute-parameter

I have some Action Attributes that allow parameters. Here's what it looks like:

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

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

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

Having an extra property inside my attribute class and executing the "Method2String" -Conversion inside my class will be fine with me if necessary to achieve what I want.

Info: I'm not looking for a way to get the name of the current method (MethodBase.GetCurrentMethod)

+5
source share
1 answer

nameof can help here:

 [DeleteActionCache(Action=nameof(GetTerms))] 

Depending on the use of the property, it might be better for you to handle this in the place where the attribute is read from the member, but in this case it seems difficult, since there is no fixed connection between the action and the property.

+7
source

Source: https://habr.com/ru/post/1264876/


All Articles