0

I am building a template in Visual Studio. The template is made of 3 C# projects. The names of the projects are as follows: name of the solution I create from my template followed by .Project1/.Project2/.Project3. For example, if I call the solution created from my template SolutionProject, the names of the projects would be: SolutionProject.Project1/.Project2/.Project3, respectively for the 3 projects. I do this with the following macro in my .vstemplate:

<ProjectCollection>
  <ProjectTemplateLink ProjectName="$projectname$.Project1">Project1\MyTemplate.vstemplate</ProjectTemplateLink>
  <ProjectTemplateLink ProjectName="$projectname$.Project2">Project2\MyTemplate.vstemplate</ProjectTemplateLink>
  <ProjectTemplateLink ProjectName="$projectname$.Project3">Project3\MyTemplate.vstemplate</ProjectTemplateLink>
</ProjectCollection>

To fix the references, which were causing issues, I used the following macro in the .csproj for each proyect. For example, for Project3, which references Project2, I used:

<ItemGroup>
<ProjectReference Include="..\$(ProjectName.Replace('.Project3', '')).Project2\$(ProjectName.Replace('.Project3', '')).Project2.csproj">
  <Project>{GUID}</Project>
  <Name>$(ProjectName.Replace('.Project3', '')).Project2</Name>
</ProjectReference>

Otherwise, the reference would be SolutionProject.Project3.Project2 instead of the desired SolutionProject.Project2.

Lastly, I have to change the using statements in my C# files. One way I have been trying to do this, is using the $safeprojectname$ macro in the .cs files, and the macro seems to work. However, I have a similar problem to the one i had with the references. If I use $safeprojectname$.Project2 macro in a .cs file in the SolutionProject.Project3 project, this is transformed to using SolutionProject.Project3.Proejct2; Note that SolutionProject is a general name and it could take any value, it adopts the name you give the project built with the template. The solution I included above that I used with the references does not work and I am running out of ideas.

  • Hello, is it convenient to share the references you mentioned? – wenbingeng-MSFT Feb 21 '23 at 09:47
  • What do you mean? – Herkules1001001 Feb 21 '23 at 10:43
  • "However, I have a similar problem to the one i had with the references.", I am referring to this references. – wenbingeng-MSFT Feb 22 '23 at 01:24
  • Yeah, I have the same issue. I have some classes in Project2 that are used in Project3, so I need a reference from Project3 to Project2. To create this reference automatically, instead of right-clicking Project3's references in the solution explorer and adding it manually, I have used a macro in Project3's .csproj file, which is the one I have provided above. I don't see the issue in sharing these references between projects, it's only an example solution with example projects. I am trying a similar approach for the using statements in the .cs files, but macros work differently in .cs files. – Herkules1001001 Feb 22 '23 at 08:26
  • Hello, I think there is a better solution for this program. For example, if I want to import project B into project A, I can right click on project A --> add reference (add reference) to select the local project, and after finding project B, add a reference. Finally use using B in the program. – wenbingeng-MSFT Feb 22 '23 at 09:51
  • Many thanks for the reply. The references between projects are already working, I do not need to change that part anymore. It's the last part that you state: "Finally use using B in the program." with what I am struggling. I know I could do this manually everytime I create a project from the template, but I would like the using statements to appear automatically every time a project is created from the template, with a different name, and the using to be customed to that name I have given the project – Herkules1001001 Feb 22 '23 at 11:05
  • Hi, glad you managed to get your code to work. If it is convenient, you can share your process and mark it as an answer so that it can help more people. – wenbingeng-MSFT Feb 23 '23 at 01:18
  • Nono, I haven't managed to get the last part of using statements to work. It's all explained in the question. I have been able to get the names of the 3 projects and the references to work using the macros I have written in the question above. My initial question was about the using statements and I haven't figured that out yet – Herkules1001001 Feb 23 '23 at 08:20
  • Hello, have you tried converting your slave project into a dll file, and then importing it into the main project and calling it? – wenbingeng-MSFT Feb 23 '23 at 09:53

0 Answers0