Visual Studio 2010 和 protobuf-csharp-port

发布于 2024-08-31 09:00:21 字数 971 浏览 5 评论 0原文

我们使用 Jon Skeet 的 proto-csharp-port,在 Visual Studio 2010 中将其与 ReSharper 混合时遇到了一些困难。

通过自定义 MSBuild 目标生成 .cs 文件,连接如下:

<Target Name="BeforeBuild" DependsOnTargets="CompileProtos" />

我们 >CompileProtos 目标运行 ProtoGen,然后使用 CreateItem 将生成的 .cs 文件添加到 @(Compile) 项组中。它会在定义的目录中查找并编译它找到的每个 .proto 文件,因此它们不会在项目中列出。

问题在于 ReSharper 无法识别 .cs 文件的内容(因为它们不在项目中并且可能还不存在),因此我无法让解决方案分析指示灯变绿。

如果我将 .cs 文件添加到项目中,则会出现构建失败,因为 .cs 文件已被添加到 Compile 项组中两次。

我知道 Marc 的 protobuf-net 有 Visual Studio 2008 优点,我正在寻找类似的东西,但是对于 Jon 的 protobuf-csharp-port 和 Visual Studio 2010。

理想情况下,我希望能够添加 .proto文件添加到项目中,正确构建它们,并让 Visual Studio 和 ReSharper 了解生成的 .cs 文件,以便 IntelliSense 和解决方案分析正常工作。

我猜想 .xsd 文件如何隐式生成 .cs 文件之类的东西就可以解决问题。

We're using Jon Skeet's proto-csharp-port, and I'm running into some difficulties when mixing it with ReSharper in Visual Studio 2010.

We generate the .cs files via a custom MSBuild target, hooked up as follows:

<Target Name="BeforeBuild" DependsOnTargets="CompileProtos" />

The CompileProtos target runs ProtoGen and then adds the generated .cs files to the @(Compile) item group, by using CreateItem. This looks in a defined directory and compiles every .proto file it finds, so they're not listed in the project.

Where it falls down is that ReSharper doesn't recognise the content of the .cs files (because they're not in the project and might not exist yet), so I can't get the solution analysis light to go green.

If I add the .cs files to the project, then I get a build failure, because the .cs file has been added to the Compile item group twice.

I know that Marc's protobuf-net has Visual Studio 2008 goodness in it, and I'm looking for something similar, but for Jon's protobuf-csharp-port and for Visual Studio 2010.

Ideally, I'd like to be able to add the .proto files to the project, have them built correctly, and have Visual Studio and ReSharper know about the generated .cs files, so that IntelliSense and solution analysis work properly.

I'm guessing that something like how .xsd files can implicitly generate .cs files would do the trick.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

权谋诡计 2024-09-07 09:00:21

我尝试通过实现 用于 的自定义工具代码生成,但我遇到了一个看似无法克服的障碍:

protoc需要一个充满.proto文件的目录并生成一个.protobin< /代码> 文件。然后将其提供给 ProtoGen,后者为每个协议定义生成一个 .cs 文件。不幸的是,.protobin 文件似乎需要包含所有定义,否则您会收到错误:无法解析所有依赖项

由于 Visual Studio 中的自定义工具模型假定单个输入文件和单个输出文件(即 foo.proto -> foo.cs),因此看起来这无法正常工作。

至少,无论如何,必须找到某种方法将 foo.proto 导入的所有 .proto 文件包含在 foo.protobin 中。

I've attempted to get this working by implementing a custom tool for code generation, but I've run into a seemingly insurmountable hurdle:

protoc takes a directory full of .proto files and generates a .protobin file. This is then fed to ProtoGen which spits out a .cs file for each protocol definition. Unfortunately, it appears that the .protobin file needs to contain all of the definitions, otherwise you get Error: Unable to resolve all dependencies.

Since the custom tool model in Visual Studio assumes a single input file and a single output file (i.e. foo.proto -> foo.cs), it doesn't look like this can be made to work.

At least, not without finding some way to include all of foo.proto's imported .proto files in foo.protobin, anyway.

沫雨熙 2024-09-07 09:00:21

我通过从 CompileProtos 目标中删除 CreateItem 并将其定义为正确的 ItemGroup 来解决这个问题:

<ItemGroup>
  <Protocols Include="$(ProtocolsPath)\*.proto"/>
</ItemGroup>
<ItemGroup>
  <Compile Include="@(Protocols -> '%(Filename).cs')"/>
</ItemGroup>

这意味着 Visual Studio(和 ReSharper )一旦构建了 .cs 文件,就可以正确拾取它们,并且 ReSharper 的完整解决方案分析将停止抱怨。

不幸的是,Visual Studio 习惯将 ItemGroup 扩展为单独的 Compile 条目,但我可以在签入任何内容之前检查这一点。

I solved it by removing the CreateItem from the CompileProtos target, and by defining it as a proper ItemGroup:

<ItemGroup>
  <Protocols Include="$(ProtocolsPath)\*.proto"/>
</ItemGroup>
<ItemGroup>
  <Compile Include="@(Protocols -> '%(Filename).cs')"/>
</ItemGroup>

This means that Visual Studio (and ReSharper) pick up the .cs files correctly, once they've been built, and ReSharper's full solution analysis stops complaining.

Unfortunately, Visual Studio has a habit of expanding the ItemGroup into individual Compile entries, but I can check for that before checking anything in.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文