使用Protobuf-net的C#项目中的协议缓冲区-代码生成的最佳实践 [英] Protocol buffers in C# projects using protobuf-net - best practices for code generation

查看:141
本文介绍了使用Protobuf-net的C#项目中的协议缓冲区-代码生成的最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用protobuf-net在C#项目中使用protobuf,并且想知道将其组织到Visual Studio项目结构中的最佳方法是什么.

I'm trying to use protobuf in a C# project, using protobuf-net, and am wondering what is the best way to organise this into a Visual Studio project structure.

当手动使用Protogen工具将代码生成到C#中时,生活似乎很轻松,但感觉并不正确.

When manually using the protogen tool to generate code into C#, life seems easy but it doesn't feel right.

我希望将.proto文件视为主要的源代码文件,但会在C#编译器介入之前将C#文件作为副产品生成.

I'd like the .proto file to be considered to be the primary source-code file, generating C# files as a by-product, but before the C# compiler gets involved.

选项似乎是:

  1. 用于原型工具的自定义工具(尽管我看不到从哪里开始)
  2. 预构建步骤(调用progen或执行此操作的批处理文件)

我在上面的2)中苦苦挣扎,因为除非我使用绝对路径(并且我不喜欢强制将项目明确定位),否则它始终提示我系统找不到指定的文件".

I have struggled with 2) above as it keeps giving me "The system cannot find the file specified" unless I use absolute paths (and I don't like forcing projects to be explicitly located).

是否有约定俗成?

根据@jon的评论,我使用Google的通讯录示例重试了预构建步骤方法,并使用了该方法(目前,该方法的位置已被硬编码):

Based upon @jon's comments, I retried the pre-build step method and used this (protogen's location hardcoded for now), using Google's address-book example:

c:\bin\protobuf\protogen "-i:$(ProjectDir)AddressBook.proto" 
       "-o:$(ProjectDir)AddressBook.cs" -t:c:\bin\protobuf\csharp.xslt


Edit2: 采纳@jon的建议以通过不处理.proto文件(如果它们没有更改)来最大程度地减少构建时间,我整理了一个基本工具来检查我(可以将其扩展为完整的Custom-Build工具):


Taking @jon's recommendation to minimise build-time by not processing the .proto files if they haven't changed, I've knocked together a basic tool to check for me (this could probably be expanded to a full Custom-Build tool):

using System;
using System.Diagnostics;
using System.IO;

namespace PreBuildChecker
{
    public class Checker
    {
        static int Main(string[] args)
        {
            try
            {
                Check(args);
                return 0;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return 1;
            }
        }

        public static void Check(string[] args)
        {
            if (args.Length < 3)
            {
                throw new ArgumentException(
                    "Command line must be supplied with source, target and command-line [plus options]");
            }

            string source = args[0];
            string target = args[1];
            string executable = args[2];
            string arguments = args.Length > 3 ? GetCommandLine(args) : null;

            FileInfo targetFileInfo = new FileInfo(target);
            FileInfo sourceFileInfo = new FileInfo(source);
            if (!sourceFileInfo.Exists) 
            {
                throw new ArgumentException(string.Format(
                    "Source file {0} not found", source));
            }

            if (!targetFileInfo.Exists || 
                sourceFileInfo.LastWriteTimeUtc > targetFileInfo.LastAccessTimeUtc)
            {
                Process process = new Process();
                process.StartInfo.FileName = executable;
                process.StartInfo.Arguments = arguments;
                process.StartInfo.ErrorDialog = true;

                Console.WriteLine(string.Format(
                     "Source newer than target, launching tool: {0} {1}",
                     executable,
                     arguments));
                process.Start();
            }
        }

        private static string GetCommandLine(string[] args)
        {
            string[] arguments = new string[args.Length - 3];
            Array.Copy(args, 3, arguments, 0, arguments.Length);
            return String.Join(" ", arguments);
        }
    }
}

现在我的预构建命令(全部在一行上):

My pre-build command is now (all on one line):

$(SolutionDir)PreBuildChecker\$(OutDir)PreBuildChecker 
    $(ProjectDir)AddressBook.proto 
    $(ProjectDir)AddressBook.cs 
    c:\bin\protobuf\protogen 
      "-i:$(ProjectDir)AddressBook.proto" 
      "-o:$(ProjectDir)AddressBook.cs" 
      -t:c:\bin\protobuf\csharp.xslt

推荐答案

作为Shaun代码的扩展,我很高兴地宣布protobuf-net现在通过自定义工具集成了Visual Studio.可从项目页中获得msi安装程序.此处提供了更完整的信息: protobuf-net;现在添加了Orcas .

As an extension of Shaun's code, I am pleased to announce that protobuf-net now has Visual Studio integration by way of a Custom Tool. The msi installer is available from the project page. More complete information here: protobuf-net; now with added Orcas.

这篇关于使用Protobuf-net的C#项目中的协议缓冲区-代码生成的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆