在 Visual Studio 中连接到构建过程 [英] Hooking into build process in Visual Studio

查看:25
本文介绍了在 Visual Studio 中连接到构建过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个 Visual Studio 扩展,它允许编辑项目中特定类型的文件.此文件用作进一步自动化代码生成的描述(类似于实体框架).

I'm writing a Visual Studio extension, which allows editing specific type of files in the project. This file serves as a description for further automated code generation (similarly to, say, Entity Framework).

我需要在构建包含描述文件的项目之前执行代码生成.代码生成算法(目前)放置在描述文件的编辑器中.

I need the code generation to be performed prior to building of the project, which contains the description file. Code generation algorithm is (currently) placed inside the editor of the description file.

有没有办法在实际构建项目之前挂钩构建过程以自动执行一些额外的步骤?

Is there a way to hook the building process to automatically perform some additional steps before actually building the project?

推荐答案

您可以使用 EnvDTE.Events.BuildEvents.OnBuildBeginOnBuildDone.

请注意,每次您说 dte.Events.BuildEvents 时,您都会在幕后创建一个新的 COM 对象,即使您仍然有一个事件侦听器,该对象也会被垃圾收集.因此,在将您的事件处理程序附加到它之前,将 BuildEvents 对象保存到某个成员变量中(这样在您使用它时就不会被垃圾收集).

Note that Every time you say dte.Events.BuildEvents you're creating a new COM object behind the scenes that is garbage collected even if you still have an event listener on it. So save the BuildEvents object into a member variable somewhere before attaching your event handlers to it (so that it doesn't get garbage collected while you're using it).

您还可以实现 Microsoft.VisualStudio.Shell.Interop.IVsBuildStatusCallback(并通过 AdviseBuildStatusCallback) 如果你厌倦了 EnvDTE :P

You could also implement Microsoft.VisualStudio.Shell.Interop.IVsBuildStatusCallback (and hook it into VS via AdviseBuildStatusCallback) if you're sick of EnvDTE :P

编辑:这两个都在 UI 线程上运行,但经过进一步思考,我认为此时修改构建本身可能为时已晚(MSBuild 可能已经发送了文件和开始异步构建).我不确定.

Edit: Both of those run on the UI thread, but upon further reflection I think it might be too late at that point to modify the build itself (MSBuild may have already been sent the files and started building asynchronously). I'm not sure.

这篇关于在 Visual Studio 中连接到构建过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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