简体   繁体   English

将类从一个 C# 项目导入另一个项目时,是否可以排除自动生成的文件?

[英]Is it possible to exclude auto-generated files when importing classes from one C# project to another?

Is it possible to exclude auto-generated files when importing classes from one C# project to another?将类从一个 C# 项目导入另一个项目时,是否可以排除自动生成的文件?

I have one project that implements a GRPC service based on classes generated from the proto file.我有一个基于从 proto 文件生成的类实现 GRPC 服务的项目。

Also, I have another project where I test classes of the first project like GRPC service.此外,我还有另一个项目,我在其中测试第一个项目的类,例如 GRPC 服务。 In the second project, I implemented a GRPC client to test the GRPC service with the same copy of the proto file.在第二个项目中,我实现了一个 GRPC 客户端,以使用相同的 proto 文件副本来测试 GRPC 服务。

The problem is that I import all classes of the first project and sorrowfully some of them are generated with the same proto file and there are a lot of warnings about class names collisions.问题是我导入了第一个项目的所有类,遗憾的是其中一些类是使用相同的 proto 文件生成的,并且有很多关于类名冲突的警告。

Is there a way to import only specific files between projects of the same solution?有没有办法在同一解决方案的项目之间仅导入特定文件?

I use the next directives in project files to generate classes:我使用项目文件中的下一个指令来生成类:

  • In the first project:在第一个项目中:
<ItemGroup>
    <Protobuf Include="Protos\Proto.proto" GrpcServices="Server" />
</ItemGroup>
  • In the second one:在第二个:
<ItemGroup>
    <Protobuf Include="Protos\Proto.proto" GrpcServices="Client" />
</ItemGroup>

And I import classes of the first project to the second one like this:我将第一个项目的类导入到第二个项目中,如下所示:

<ItemGroup>
    <ProjectReference Include="..\core\First.csproj" />
</ItemGroup>

And the warnings are like this:警告是这样的:

TestClient.cs(53,29): warning CS0436: The type 'Type1' in '/folder1/solution/test_project/obj/Debug/netcoreapp3.1/Class1.cs' conflicts with the imported type 'Type1' in 'Project1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. Using the type defined in '/folder1/solutiion/test/obj/Debug/netcoreapp3.1/Class1.cs'. [/folder1/solution/test_project/Project2.csproj]

I usually create my proto files and their resulting generated code in a single assembly, then reference that assembly from the service and the client assemblies.我通常在单个程序集中创建我的 proto 文件及其生成的代码,然后从服务和客户端程序集中引用该程序集。 This keeps the code generated once, and eliminates the duplicated code generation.这保留了一次生成的代码,并消除了重复的代码生成。

You can't conditionally import the types, but you can add an extern alias to the types in your First project when importing it to the second project.您不能有条件地导入类型,但可以在将第一个项目导入到第二个项目时为第一个项目中的类型添加extern 别名

Add the following snippet to your second .csproj将以下代码段添加到您的第二个 .csproj

  <Target Name="ChangeAliasesOfStrongNameAssemblies" BeforeTargets="FindReferenceAssembliesForReferences;ResolveReferences">
    <ItemGroup>
      <ReferencePath Condition="'%(FileName)' == 'First'">
        <Aliases>FirstExtern</Aliases>
      </ReferencePath>
    </ItemGroup>
  </Target>

you can now explicitly define which type you want to use: add this above all the usings in your .cs code:您现在可以明确定义要使用的类型:将其添加到 .cs 代码中的所有用途之上:

extern alias FirstExtern;

and reference the types from the first project by specifying the defined alias:并通过指定定义的别名引用第一个项目中的类型:

FirstExtern::First.Type1 variable;

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM