Home > Net >  C# Use classes in projects from Solution level
C# Use classes in projects from Solution level

Time:10-09

In this current Solution I will have ~10 projects (Console Applications) which will use the same functions from Helper classes. In the past I would just copy paste those folders in each project and add the references to the main class. Is there a way to create the Helper classes only once and be able to use them in any project in that solution, without duplicating them or having to reference another project?

Also I have to import the same 3 NuGet packages in each project, is there a way to import them at solution level and so they will be automatically added in each new project? I know I can right-click the Solution and just select each project without doing it manually for each project, but can they be added automatically for each new project created?

CodePudding user response:

You need edit csproj, for example you have following directory structure:

SolutionRoot
  MyProject
    MyProject.csproj
  SharedFiles

To add files in SharedFiles directory into MyProject project as a virtual folder named Shared, add following lines into MyProject.csproj

<ItemGroup>
   <Compile Include="..\SharedFiles\**\*.cs">
      <Link>Shared\%(RecursiveDir)\%(FileName)%(Extension)</Link>
   </Compile>
</ItemGroup>

CodePudding user response:

Is there a way to create the Helper classes only once and be able to use them in any project in that solution, without duplicating them or having to reference another project?

Simple answer no. You must define your helper classes in a project that can be referenced in your other console applications.

Also I have to import the same 3 NuGet packages in each project, is there a way to import them at solution level and so they will be automatically added in each new project? I know I can right-click the Solution and just select each project without doing it manually for each project, but can they be added automatically for each new project created?

Again no, you can't apply NuGet references to new projects at a solution level. However, you can create your own project template which can be setup to have package/project references configured.

  • Related