C# 如何使用方法参数属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17150365/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use method parameter attributes
提问by Ross
I've been struggling to find examples of how to write a custom attribute to validate method parameters, i.e., turn this form:
我一直在努力寻找如何编写自定义属性来验证方法参数的示例,即,转换以下形式:
public void DoSomething(Client client)
{
if (client.HasAction("do_something"))
{
// ...
}
else
{
throw new RequiredActionException(client, "do_something");
}
}
into this:
进入这个:
public void DoSomething([RequiredAction(Action="some_action")] Client client)
{
// ...
}
As far as I can tell, I need to add this attribute to my custom attribute, but I'm at a loss on how to access the decorated parameter Client:
据我所知,我需要将此属性添加到我的自定义属性中,但我不知道如何访问装饰参数Client:
[AttributeUsageAttribute(AttributeTargets.Parameter)]
public class RequireActionAttribute : System.Attribute
{
public Type Action {get; set;}
public RequireActionAttribute()
{
// .. How do you access the decorated parameter?
Client client = ???
if (!client.HasAction(Action))
{
throw new RequiredActionException(client, Action);
}
}
}
回答by Jon Skeet
You're applying it correctly - but an attribute basically doesn't know the member it refers to. This definitely makes life harder.
您正确地应用了它 - 但一个属性基本上不知道它所指的成员。这无疑让生活变得更加艰难。
Not only does it not have access to the member that it refers to, but that member would be a ParameterInfo, not a Client- there's no easy way of accessing the valueof a parameter externally. Your method would need to call some helper code, passing the value of clientin order to handle it appropriately... or you need to hook into the code which is going to call your method to start with, in order to notice the attribute.
它不仅不能访问它所引用的成员,而且该成员将是 a ParameterInfo,而不是 a Client- 没有简单的方法可以从外部访问参数的值。您的方法需要调用一些帮助程序代码,传递 的值client以便适当地处理它……或者您需要挂接到将要调用您的方法的代码中,以便注意到该属性。
It's not clear exactly how you were hoping to use this, but it may well be that you need to change your design significantly.
目前尚不清楚您希望如何使用它,但很可能您需要显着改变您的设计。
回答by Tony
Attributes probably should be put on the method itself. When I was searching for the solution I found the following link and the way it uses interceptor seems even better http://www.codinginstinct.com/2008/05/argument-validation-using-attributes.html
属性可能应该放在方法本身上。当我寻找解决方案时,我发现了以下链接,它使用拦截器的方式似乎更好http://www.codinginstinct.com/2008/05/argument-validation-using-attributes.html
回答by silver
Attributes are not enough for doing it.
属性是不够的。
If I understood you correctly you want to add an attribute on a parameter in order to validate it at run time and that is impossible only with attributes.
如果我理解正确,您想在参数上添加一个属性以便在运行时对其进行验证,而这仅使用属性是不可能的。
It is impossible because attributes are only "metadata" and not executed code.
这是不可能的,因为属性只是“元数据”而不是执行的代码。
You will need some "real" code to read it and act accordingly. That code can be injected at compile time or you can hook into the function execution.
您将需要一些“真正的”代码来阅读它并采取相应的行动。该代码可以在编译时注入,也可以挂接到函数执行中。

