[英]how to include a shared class between several project in visual studio code
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.cs
和ProjectB\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.csproj
和ProjectB\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.像往常一样创建ProjectA和ProjectB 。 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 风格项目
<Project>
<PropertyGroup>
<CoreCompileDependsOn>InitGlobal;$(CoreCompileDependsOn)</CoreCompileDependsOn>
</PropertyGroup>
</Project>
<Project>
<Target Name="InitGlobal" >
<ItemGroup>
<Compile Include="$(MSBuildThisFileDirectory)\Global\*.cs" />
</ItemGroup>
</Target>
</Project>
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.