Sign in to follow this  
GuyWithBeard

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

Recommended Posts

GuyWithBeard    1892
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 this post


Link to post
Share on other sites
mutex    1111
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 this post


Link to post
Share on other sites
GuyWithBeard    1892
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 this post


Link to post
Share on other sites
Nypyren    12073
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 this post


Link to post
Share on other sites
GuyWithBeard    1892
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!

Share this post


Link to post
Share on other sites
benryves    1999
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 this post


Link to post
Share on other sites
Telastyn    3777
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.

Share this post


Link to post
Share on other sites
GuyWithBeard    1892
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this