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.

9
  • Hello, is it convenient to share the references you mentioned? Commented Feb 21, 2023 at 9:47
  • What do you mean? Commented Feb 21, 2023 at 10:43
  • "However, I have a similar problem to the one i had with the references.", I am referring to this references. Commented Feb 22, 2023 at 1: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. Commented Feb 22, 2023 at 8: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. Commented Feb 22, 2023 at 9:51

0

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.