# [.net] [C#] Modifying funtion parameters using attributes.

## Recommended Posts

Hi! Is there a way to automatically modify parameters pass to a function using attributes? I was thinking about something like this: [CapitalizedStrings()] void TestFunction(string param1, string param2, int param3) { Console.WriteLine(param1 + param2 + param3.ToString()); } The CapitalizedStrings attribute would make param1 and param2 capitalized automatically. Thanks!

##### Share on other sites
No. Attributes are merely addition information to code; they do not alter behavior by themselves. You'd have to write something that discovers those attributes and alters parameters accordingly, but then it is no longer automatic.

##### Share on other sites
Allright, what then would be the most elegant way to mark certain functions in a class so that all their string parameters get capitalized (semi-)automatically? I would not want to go through all the parameters in every function myself.

##### Share on other sites
This is what first comes to mind. Replace your parameters with:

public class CapString{  string str;  public CapString(string foo)  {    str = // do whatever you want to do to capitalize foo  }  public static implicit operator string(CapString capstr)  {    return capstr.str;  }  public static implicit operator CapString(string foo)  {    return new CapString(foo);  }}

You might also try changing it from a class to a struct if you want it to act like a value type.

##### Share on other sites
Big waste of time. Never re-invent the wheel unless you've got a really good reason. Check this out: String.ToUpper Method

hth,

~Shiny

##### Share on other sites
Quote:
 Original post by ShinyBig waste of time. Never re-invent the wheel unless you've got a really good reason. Check this out: String.ToUpper Methodhth,~Shiny

Yeah, I know. The capitalization was just an example, sorry if I made it unclear. Currently use the technique suggested by Nypyren to modify the strings according to some preset patterns, and it works very well. Thanks!

##### Share on other sites
Caveat for string.ToUpper() - not all cultures capitalise strings the same way. A good example is the letter i - in English the lowercase form is dotted and the uppercase form is dotless. However, when dealing with a Turkish culture, there is both a dotted and dotless lower case and uppercase i.

If appropriate, you can avoid the issue by using string.ToUpperInvariant(), which will use the rules according to the invariant culture (which roughly matches an English culture).

##### Share on other sites
Quote:
 Original post by NypyrenThis is what first comes to mind. Replace your parameters with:

I am very nearly certain that you cannot do that. Making conversion operators both from and to a type from another type is a compile time error.

##### Share on other sites
Quote:
Original post by Telastyn
Quote:
 Original post by NypyrenThis is what first comes to mind. Replace your parameters with:

I am very nearly certain that you cannot do that. Making conversion operators both from and to a type from another type is a compile time error.

Well, it works fine for us.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628288
• Total Posts
2981846

• 11
• 10
• 10
• 11
• 17