How to use method parameter attributes
Solution 1
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 value of a parameter externally. Your method would need to call some helper code, passing the value of client
in 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.
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.
Solution 2
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.
Solution 3
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
Ross
Updated on July 09, 2022Comments
-
Ross almost 2 years
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
:[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); } } }
-
Andre Calil almost 11 yearsWhy can't you simply accept an interface as the parameter? For example, you could have an
IDoSomething
. -
Adam Houldsworth almost 11 yearsYou cannot access the decorated item from inside the attribute. Attributes sit against the type meta-data statically, so you actually get the attribute from the item. That said, the attribute could then have a method that takes an argument that is the item, but then you're using a sledgehammer to crack a walnut and could have done it a lot easier without attributes.
-
-
Dieter Meemken about 8 yearsCan you explain a bit more?
-
Tony about 8 yearsI might have misunderstood the original code. The link I posted uses interceptor to validate code. Comment doesn't allow me to post code from there. It just looks to me that this solution looks more elegant than the attribute validation we did in the past.