Sign in to follow this  

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

This topic is 3617 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
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
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
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
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
Sign in to follow this