Visual Studio Templates - custom parameters

ぃ、小莉子 提交于 2020-02-07 05:21:24

问题


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 this - MyTemplatedProject. I use $projectname$ for the part of class names and namespaces.

If I create generate a class like $projectname$Context, it becomes MyTemplatedProjectContext and all is well.

But if I name my project MyTemplatedProject.FrontEnd I have problems with the classes that are generated because they have the . in their name.

The substitution $projectname$Context becomes MyTemplatedProject.FrontEndContext and that does not work for a class name.

How do I create custom parameters based $projectname$, ideally I would have a parameter like $projectnameUpToFirstDot$ which only returns MyTemplatedProject when the project name is MyTemplatedProject.FrontEndContext?


回答1:


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.




回答2:


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] {
    ...
}


来源:https://stackoverflow.com/questions/37395075/visual-studio-templates-custom-parameters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!