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

Started by
7 comments, last by GuyWithBeard 16 years, 2 months ago
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!
Advertisement
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.

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.
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.
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
------------'C makes it easy to shoot yourself in the foot. C++ makes it harder, but when you do, it blows away your whole leg.' -Bjarne Stroustrup
Quote:Original post by Shiny
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


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!
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).

[Website] [+++ Divide By Cucumber Error. Please Reinstall Universe And Reboot +++]

Quote:Original post by Nypyren
This 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.
Quote:Original post by Telastyn
Quote:Original post by Nypyren
This 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.

This topic is closed to new replies.

Advertisement