我有一些允许参数的 Action 属性。它是这样的:
[DeleteActionCache(Action="GetTerms")]
public ActionResult AddTerm(string term){ }
public ActionResult GetTerms() {}
现在,我想摆脱属性中的魔术字符串“GetTerms”。所以我更喜欢这样的东西:(伪代码,不起作用)
[DeleteActionCache(Action=this.GetTerms().GetType().Name)]
如果需要这样做来实现我想要的功能,那么在我的属性类中具有一个附加的Property并在该类中进行“Method2String” -Conversions转换就可以了。
信息:我是,而不是,正在寻找一种获取当前方法名称(MethodBase.GetCurrentMethod)的方法
最佳答案
nameof
可以在这里提供帮助:
[DeleteActionCache(Action=nameof(GetTerms))]
根据属性的使用,可能最好在从成员读取属性的地方进行处理,但是在这种情况下,由于操作和属性之间没有固定的关系,因此似乎很难。