Visual Studio Templates - custom parameters

前端 未结 2 1973
终归单人心
终归单人心 2021-01-28 18:02

I am using VS 2015 templates to create a project for me, within my C# classes I have substitutions like $projectname$ and they work great if I name my project like

相关标签:
2条回答
  • 2021-01-28 18:25

    If you just want to generate a valid class name based on the project name, instead of creating a custom template parameter, you can change $projectname$ to $safeprojectname$, which is documented as:

    The name provided by the user in the New Project dialog box, with all unsafe characters and spaces removed.

    0 讨论(0)
  • 2021-01-28 18:28

    I had exactly the same problem a week ago. I just needed some customed variables inside of my template, which would resolve in $projectname$ upper case and one other in lower case. Things like that.

    It turned out for me to be the most effective and flexible way to develop my own project wizard using VS' custom wizard template.

    Unless JavaScript & HTML is not a no-go for you, it is pretty easy to have some string manipulation on variables like $projectname$ there.

    Once you set it up, go in default.js and edit in function

    function onFinish()
    {
        ...
    
        var prjName = wizard.FindSymbol('PROJECT_NAME');
    
        wizard.AddSymbol('PROJECT_NAME_WITHOUT_DOTS', prjName.split('.')[0]);
    
        ...
    }
    

    split('.') removes all dots and devides it into an array of strings. With [0] you select just the part up to the first dot.

    In your template files you can then address the symbol PROJECT_NAME_WITHOUT_DOTS with the following syntax:

    class [!output PROJECT_NAME_WITHOUT_DOTS] {
        ...
    }
    
    0 讨论(0)
提交回复
热议问题