Answered by:
Attribute on a particular parameter of a method?

Question
-
Would it be possible to define attributes on select parameters of a method? I don't want the attribute on the method name itself.Wednesday, August 8, 2012 5:46 PM
Answers
-
Make a overload of the method without the param which u want to obsolete / remove and mark the old one as obsolete.
--Krishna
- Proposed as answer by MasaSam Wednesday, August 8, 2012 6:21 PM
- Marked as answer by Deepak Vasudevan Wednesday, August 8, 2012 7:46 PM
Wednesday, August 8, 2012 6:13 PM
All replies
-
Yes - you can define attributes on a method parameter. For example, the new .NET 4.5 CallerMemberNameAttribute has its Attribute Targets set to param, so you use it like:
public void PrintCaller([CallerMemberName] string memberName = "") { Console.WriteLine("Method called by {0}", memberName); }
Reed Copsey, Jr. - http://reedcopsey.com
If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".Wednesday, August 8, 2012 5:50 PM -
Yes. The below attribute can be applied to parameters.
[AttributeUsage(AttributeTargets.Parameter)] class MyAttribute : Attribute { }
I hope this helps.Please mark this post as answer if it solved your problem. Happy Programming!
- Proposed as answer by MasaSam Wednesday, August 8, 2012 6:21 PM
- Unproposed as answer by Deepak Vasudevan Wednesday, August 8, 2012 7:46 PM
Wednesday, August 8, 2012 5:54 PM -
Let me make things clear.
I would like to add an Obsolete attribute on one particular parameter alone.
Wednesday, August 8, 2012 6:09 PM -
Make a overload of the method without the param which u want to obsolete / remove and mark the old one as obsolete.
--Krishna
- Proposed as answer by MasaSam Wednesday, August 8, 2012 6:21 PM
- Marked as answer by Deepak Vasudevan Wednesday, August 8, 2012 7:46 PM
Wednesday, August 8, 2012 6:13 PM -
If parameter is considered obsolete, then do as Krishnav has said. Make overload without obsolete parameter and mark the whole method with useless parameter obsolete.
If you need apply attribute to property, the sample attribute class by Adavesh will work, but as said don't use it to mark obsolete parameters.
Wednesday, August 8, 2012 6:20 PM -
Krishnav's suggestion is the appropriate one here.
That being said, this won't work - as ObsoleteAttribute's AttributeTargets don't include AttributeTargets.Parameter. Without this, you can't apply it to a parameter.
However, you wouldn't really be able to anyways - there's no way to use a method and not use one parameter of the method - so it would always be tagged Obsolete. The problem is that Optional parameters are resolved and "filled in" at compile time, so, when you compile, the optional parameter actually still gets a value (the default value). James Michael Hare blogged about this in his pros and cons of using optional arguments: http://geekswithblogs.net/BlackRabbitCoder/archive/2010/06/17/c-optional-parameters---pros-and-pitfalls.aspx
Reed Copsey, Jr. - http://reedcopsey.com
If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".Wednesday, August 8, 2012 6:42 PM -
Thanks Krishna.
Actually I like this approach because it wouldn't break any older clients too. For some reason, I never envisaged this route.
Wednesday, August 8, 2012 7:47 PM