1

I'm generating C# code in C# and I'd like to format string as proper C# class. Are there any libraries to do that? I need to replace forbidden characters, PascalCase string (between "_") etc.

I'm passing parameter "ClassName" to T4 template and I'd like to convert that parameter to proper C# class name.

ex.

Given:

ClassName = "This_is_MY_CLASS Name"

Then: I'd like to get

ProperClassName = "This_Is_My_Class_Name" // or "This_Is_My_Classname", both would be acceptable.
4
  • go with FXCop and / or stylecop. Commented Jan 9, 2013 at 8:39
  • Not exactly what I want. I get string as parameter for my T4 template. This parameter, lets say it's ClassName, should be set by user and I'd like to prevent bad names. When someone pass something like "This_IS_my ClassName", I'd like to get "This_Is_My_ClassName" or "This_Is_My_Classname" would be also acceptable. I need to do this during generating code, so I don't know if StyleCop/FxCop can do that. Are there any tools/libs in StyleCope/FxCope to deal with it? Or there are just methods that say "Class name is wrong". I get such error during compilation, so it's not useful at all. Commented Jan 9, 2013 at 8:41
  • I think a common solution is to further restrict the set of allowable class names (i.e. not allow the full range of identifiers that would be valid in C#), so it gets possible to handle manually. e.g. only alphanumeric characters and underscores, no accented characters or characters from non-Latin alphabets. Commented Jan 9, 2013 at 8:53
  • 1
    And what's with all the close votes for being off-topic? How is this question not a programming-related question within the scope of SO? Commented Jan 9, 2013 at 9:23

2 Answers 2

1

You could possibly use the TextInfo class

string className = "This_is_MY_CLASS Name";

string newName = new CultureInfo(CultureInfo.CurrentCulture.LCID, false).TextInfo.ToTitleCase(className .ToLower()).Replace(" ","_");

returns: "This_Is_My_Class_Name"

if you want to remove most illegal chars you could do something like this

    string className = "This_is_MY_CLASS Name/with<silly|chars";
    var invalid = System.IO.Path.GetInvalidFileNameChars().Concat(new char[]{' '});

    string newName = new string( new CultureInfo(CultureInfo.CurrentCulture.LCID, false)
        .TextInfo.ToTitleCase(className.ToLower())
        .Select(s => invalid.Contains(s) ? '_' : s).ToArray());

   // returns: "This_Is_My_Class_Name_With_Silly_Chars"
Sign up to request clarification or add additional context in comments.

1 Comment

I'm already doing the first part, but I thought that maybe there are some refactoring libraries or something like that, which could automagically remove invalid characters instead of doing it manually, like in the second part
0

Maybe the ToTitleCase method will take some work for you, but for removing illegal characters and the other stuff you have to write something on your own. Within the .net framework is nothing available out of the box.

1 Comment

I'm using ToTitleCase already. I just wanted to be sure that there is nothing available. I don't like to reinvent the wheel, so thx.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.