简体   繁体   English

如何在 Visual Studio 代码中的多个项目之间包含共享的 class

[英]how to include a shared class between several project in visual studio code

I am trying to include a shared class between 2 projects, but i can't seem to find out how add them to the project visual studio code, so i can use them.我试图在 2 个项目之间包含一个共享的 class,但我似乎不知道如何将它们添加到项目 Visual Studio 代码中,所以我可以使用它们。 I hope this image helps explain I am trying to ask我希望这张图片有助于解释我想问的问题

文件夹结构的外观

I'm not sure for what reason you'd want to have the source code shared between projects instead of the resulting compiled assembly , but given this directory structure...我不确定出于什么原因您希望在项目之间共享源代码而不是生成的编译程序集,但是鉴于此目录结构...

  • ProjectA\
    • ProjectA.csproj
    • Program.cs
  • ProjectB\
    • ProjectB.csproj
    • Program.cs
  • Shared\
    • Shared.cs

...I was able to use a shared code file ( Shared\Shared.cs ) like this... ...我能够像这样使用共享代码文件( Shared\Shared.cs )...

namespace Shared
{
    internal static class Example
    {
        internal static void SayHello()
        {
            string assemblyName = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;

            System.Console.WriteLine($"Hello, World!  This is {assemblyName}.");
        }
    }
}

...from console application code files ( ProjectA\Program.cs and ProjectB\Program.cs ) like this... ...来自这样的控制台应用程序代码文件( ProjectA\Program.csProjectB\Program.cs )...

namespace ProjectA // or ProjectB
{
    class Program
    {
        static void Main()
        {
            Shared.Example.SayHello();
        }
    }
}

...by modifying the default console application project files ( ProjectA\ProjectA.csproj and ProjectB\ProjectB.csproj ) like this to include all .cs files from the Shared\ directory... ...通过像这样修改默认控制台应用程序项目文件( ProjectA\ProjectA.csprojProjectB\ProjectB.csproj )以 包含Shared\目录中的所有.cs文件...

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp3.1</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <Compile Include="..\Shared\*.cs" />
  </ItemGroup>
</Project>

...which yielded these results when run from the base directory... ...从基本目录运行时产生了这些结果...

PS> dotnet run --project ProjectA
Hello, World!  This is ProjectA.
PS> dotnet run --project ProjectB
Hello, World!  This is ProjectB.

Interestingly, I ran the dotnet msbuild command like this...有趣的是,我像这样运行了dotnet msbuild命令......

dotnet msbuild ProjectA -preprocess:fullproject.xml

...to produce a file showing the behind-the-scenes definitions MSBuild would use, and it included a comment describing this same hierarchy with even the same sample names I used... ...生成一个文件,显示 MSBuild 将使用的幕后定义,它包含一个注释,描述了这个相同的层次结构,甚至使用了相同的示例名称......

<!-- WARNING: This pattern is there to ignore folders such as .git and .vs, but it will also match items included with a
      relative path outside the project folder (for example "..\Shared\Shared.cs").  So be sure only to apply it to items
      that are in the project folder. -->
<DefaultExcludesInProjectFolder>$(DefaultItemExcludesInProjectFolder);**/.*/**</DefaultExcludesInProjectFolder>

Maybe not a good solution, but another solution would be to use symlinks/hardlinks/junctions to include the shared code file(s)/directory directly in each project like this...也许不是一个好的解决方案,但另一种解决方案是使用符号链接/硬链接/连接将共享代码文件/目录直接包含在每个项目中,就像这样......

  • ProjectA\
    • ProjectA.csproj
    • Program.cs
    • Shared.cs..\Shared\Shared.cs Shared.cs..\Shared\Shared.cs
  • ProjectB\
    • ProjectB.csproj
    • Program.cs
    • Shared.cs..\Shared\Shared.cs Shared.cs..\Shared\Shared.cs
  • Shared\
    • Shared.cs

...or this... ...或这个...

  • ProjectA\
    • ProjectA.csproj
    • Program.cs
    • Shared\..\Shared\ Shared\..\Shared\
      • Shared.cs
  • ProjectB\
    • ProjectB.csproj
    • Program.cs
    • Shared\..\Shared\ Shared\..\Shared\
      • Shared.cs
  • Shared\
    • Shared.cs

...although I could see that causing some headaches with source control. ...尽管我可以看到这会导致源代码控制有些头疼。

In all of the above examples I used a convenient base directory and relative paths, but there's no reason you couldn't use absolute paths, if needed.在上述所有示例中,我使用了方便的基本目录和相对路径,但如果需要,没有理由不能使用绝对路径。

Create your ProjectA and ProjectB as you would normally do.像往常一样创建ProjectAProjectB Then create a shared project, say CommonProject .然后创建一个共享项目,比如CommonProject Once the shared project is referenced into the ProjectA and ProjectB, you should be able to access all the shared classes from all the referenced projects.一旦共享项目被引用到 ProjectA 和 ProjectB 中,您应该能够访问所有引用项目中的所有共享类。 Also you can include conditional compilations symbols within the class to suit the class definition per project.您还可以在 class 中包含条件编译符号,以适应每个项目的 class 定义。

I think that first you should create an .NET Standard Class Library Project and reference it like this: Reference image我认为首先你应该创建一个 .NET 标准 Class 库项目并像这样引用它:参考图像

For .NET Core & SDK style projects对于 .NET Core & SDK 风格项目

Add Directory.Build.props to solution folder (ie- a parent folder of both projects):将 Directory.Build.props 添加到解决方案文件夹(即两个项目的父文件夹):

<Project>  
    <PropertyGroup>
        <CoreCompileDependsOn>InitGlobal;$(CoreCompileDependsOn)</CoreCompileDependsOn>
    </PropertyGroup>
</Project>

Add Directory.Build.targets to solution folder (ie- a parent folder of both projects):将 Directory.Build.targets 添加到解决方案文件夹(即两个项目的父文件夹):

<Project>  
    <Target Name="InitGlobal" >
        <ItemGroup>
            <Compile Include="$(MSBuildThisFileDirectory)\Global\*.cs" />
        </ItemGroup>
    </Target>
</Project>

Finally最后

Add the shared/common class file(s) to \Global folder (relative to the Directory.Build.* files above).将共享/通用 class 文件添加到 \Global 文件夹(相对于上面的 Directory.Build.* 文件)。 Careful that any dependencies used are included in each project, or that you add the dependencies in the Directory.Build.props.小心使用的任何依赖项都包含在每个项目中,或者在 Directory.Build.props 中添加依赖项。

Note - the global files will be included in the Build and the IntelliSense, but not visible in the solution.注意 - 全局文件将包含在 Build 和 IntelliSense 中,但在解决方案中不可见。 You can make them show as linked classes, by removing the element from the.targets and and putting it in the.props file您可以通过从.targets 中删除元素并将其放入.props 文件中,使它们显示为链接类

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Visual Studio 2015 中的共享项目和类库有什么区别? - What is the difference between a Shared Project and a Class Library in Visual Studio 2015? Visual Studio:如何管理项目之间共享的代码 - Visual Studio : How to manage code shared between projects 为什么用户之间不能共享Visual Studio项目属性? - Why are Visual Studio project properties not shared between users? 如何在.NET核心类库中引用Visual Studio共享项目 - How do I reference a Visual Studio Shared Project in a .NET Core Class Library 如何在1个Launcher项目中合并几个Visual Studio项目 - How to merge several Visual Studio projects in 1 Launcher project Visual Studio:如何将代码模板添加到项目 - Visual Studio: How to add code templates to project Visual Studio项目:如何仅包括一种配置的参考? - Visual Studio Project: How to include a reference for one configuration only? 如何在Visual Studio项目中包含网络网站源文件 - how to include net web site source files in Visual Studio project 如何在不依赖Visual Studio C#的生成中包含项目 - How to include a project in a build that is not a dependency in Visual Studio c# 如何在 Visual Studio、C# 项目的部署中包含文件和文件夹? - How to include files and folders in the deployment of a visual studio, C# project?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM