Adding a custom format to template settings Visual Studio project

I defined a Visual Studio template called classDB.cs . I would like the default name for the class to display as [projectname] DB.cs, where [project_name] is the name of the current project (as indicated in the Create Project dialog box). Is there any way to achieve this? I tried setting the class name to $safeprojectname$DB.cs , but that didn't work.

UPDATE

I changed my project template, but gave this error when creating a project

enter image description here

here is the template class

 namespace $safeprojectname$.Models { public class $safeprojectname$DB : DbContext { } } 

the class in the project

+4
source share
3 answers

I struggled with a similar error for several days, and I finally figured it out. Visual Studio avoids $ in the .csproj file. So you will have a node that looks like this:

 <Compile Include="Models\%24safeprojectname%24DB.cs" /> 

Open the .csproj file in a text editor and change it to:

 <Compile Include="Models\$safeprojectname$DB.cs" /> 

And save the file. Your project will restart, but it will not try to avoid the file name again! Export your template and you should find that the parameter is now being replaced.

+3
source

Try a template like this:

 using System; //... namespace $rootnamespace$ { class $safeitemname$DB { } } 

It works for me.

Make sure you update the correct template (should be located in C:\Users\[user]\Documents\Visual Studio 2010\Templates\ItemTemplates in Windows 7) and restart Visual Studio.


EDIT

The above code is for an element template, but this should not differ from the project template. According to MSDN, the parameters $safeitemname$ and $safeprojectname$ behave the same:

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

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

+2
source

For the case when someone needs this:

I tried to add an interface to the class and did not know how to get the interface name to be composed of the class name, such as I + $fileinputname$ .


In the *.vstemplate file, just use I$fileinputname$.cs for the TargetFileName interface:

 <TemplateContent> <ProjectItem TargetFileName="$fileinputname$.cs" ReplaceParameters="true">SomeClass.cs</ProjectItem> <ProjectItem TargetFileName="I$fileinputname$.cs" ReplaceParameters="true">ISomeClass.cs</ProjectItem> </TemplateContent> 

In the ISomeClass.cs file ISomeClass.cs again use I$fileinputname$ for the interface name:

 namespace $rootnamespace$ { public interface I$fileinputname$ { } } 

Important:

Set the action of assembling the SomeClass.cs and ISomeClass.cs to None , since this project is intended to create a .zip file, not a .dll or .exe file, the code files are not compiled or are not considered as code for the purposes of the assembly ( .vstemplate file has VSTemplate assembly VSTemplate .).


As a result, when SomeClass.cs added as an Item template , the corresponding ISomeClass.cs interface is ISomeClass.cs .

enter image description here

-1
source

Source: https://habr.com/ru/post/1379670/


All Articles