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

时间:2022-05-14 20:36:35

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)

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

[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.

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

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

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

1 个解决方案

#1


7  

nameof could help here:

nameof可以帮助:

[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.

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

#1


7  

nameof could help here:

nameof可以帮助:

[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.

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