Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a practical example of how they have used attributes on method parameters in .NET?

Tags:

c#

.net

I know it's possible, and I've seen simple examples in the documentation, but are they being used in the wild?

I use attributes at the class and method level all the time, but have never used them on method parameters. What are some real-world examples, and the reasons for the usage?

I'm not interested in seeing a textbook example, mind you. There are plenty of those out there. I want to see an actual reason why it solved a particular problem for you.

EDIT: Let's place aside the discussion about whether or not to use attributes in the first place. I understand some people don't like them because they "dirty" their code. That's for a different discussion!

like image 623
Kilhoffer Avatar asked Sep 26 '08 14:09

Kilhoffer


1 Answers

You can for example create a ValidatorAttribute for every parameter, then before calling the method, you can reflect the parameter attributes and do parameter validation. Then call the method if all ok.

like image 53
leppie Avatar answered Sep 19 '22 09:09

leppie