2017-03-01 94 views
5

我有一些允许参数的动作属性。这是它的样子:获取行为名称作为属性参数的字符串

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

public ActionResult GetTerms() {} 

现在我想摆脱我的属性中的魔术字符串“GetTerms”。所以,我宁愿是这样的:(伪代码,不工作)

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

有我的属性级内的附加属性和做“Method2String” -Conversions在我的这个类将是确定与我,如果这是需要达到我想要的。

信息:我寻找一种方式将得到当前方法名(MethodBase.GetCurrentMethod

回答

7

nameof可以帮助在这里:

[DeleteActionCache(Action=nameof(GetTerms))] 

根据不同的使用你可能会更好地在属性从成员中读取的地方处理它,但在这种情况下,似乎很难,因为动作之间没有固定的关系和财产。

相关问题