获取操作名称作为Attribute-parameter的字符串

Ole*_*ers 5 c# reflection methods asp.net-mvc attributes

我有一些允许参数的动作属性.这是它的样子:

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

public ActionResult GetTerms() {}
Run Code Online (Sandbox Code Playgroud)

现在我想摆脱属性中的魔术字符串"GetTerms".所以我更喜欢这样的东西:(伪代码,不工作)

[DeleteActionCache(Action=this.GetTerms().GetType().Name)]
Run Code Online (Sandbox Code Playgroud)

在我的属性类中有一个额外的属性并且在我的那个类中进行"Method2String" - 转换对我来说没关系,如果需要它来实现我想要的东西.

信息:我不是在寻找获取当前方法名称的方法(MethodBase.GetCurrentMethod)

Pat*_*man 7

nameof 可以帮到这里:

[DeleteActionCache(Action=nameof(GetTerms))]
Run Code Online (Sandbox Code Playgroud)

根据属性的使用情况,您最好在从成员读取属性的位置处理此属性,但在这种情况下,由于操作和属性之间没有固定的关系,因此看起来很难.