Visual Studio 代码生成,基于项目的内容

发布于 2024-07-29 18:44:19 字数 201 浏览 4 评论 0原文

我有兴趣根据 .csproj 中存在的某些文件进行一些代码生成。 我可以使用哪些扩展方法来生成与我的项目一起编译的 .cs 文件?

警告:我立即想到使用 T4 模板来完成该任务。 但是,此解决方案必须在 Visual Studio C# Express 上受支持。 我认为express版本不支持T4模板

I'm interested in doing a bit of code-generation based on certain files being present in the .csproj. What extensibility methods are available to me that I could generate a .cs file that would be compiled along with my project?

Caveat: I immediately thought of using T4 templates for the task. However, this solution must be supportable on Visual Studio C# Express. I believe that the express version doesn't support T4 templates

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

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

发布评论

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

评论(2

深居我梦 2024-08-05 18:44:19

在 C# Express 上,答案很简单:没有。

C# Express 不支持扩展。 人们以前曾尝试过,但效果却很丑陋。 律师,混乱,一切。 著名的例子是 TestDriven。 NET。

如果您不介意在命令行进行构建,则可以使用 msbuild 自定义任务或预构建/构建后命令。

在 VS2005/VS2008(“正确的”)中,“自定义工具”是的方法之一; 这涉及编写一个Package我最近完成了此操作 ,并且代码已开放检查。

T4 是未来的另一个选择。 它不符合我的需求,但可能适合你的需求。


作为对构建事件的快速检查,我分别添加了(在快速项目中)预构建和后构建事件(项目属性 -> 构建事件):

echo $(TargetPath)

echo $(SolutionDir)

; 点击构建,现在的输出是:

------ Build started: Project: ConsoleApplication10, Configuration: Release Any CPU ------
echo D:\SomePath\ConsoleApplication10.exe
D:\SomePath\ConsoleApplication10.exe
c:\WINDOWS\Microsoft.NET\Framework\v3.5\Csc.exe /noconfig /unsafe+ /nowarn:1701,1702 /errorreport:prompt /warn:4 /define:TRACE /reference:"c:\Program Files\Reference Assemblies\Microsoft\Framework\v3.5\System.Core.dll" /reference:"c:\Program Files\Reference Assemblies\Microsoft\Framework\v3.5\System.Data.DataSetExtensions.dll" /reference:c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.Data.dll /reference:c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.Design.dll /reference:c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.dll /reference:c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.Drawing.dll /reference:c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.Windows.Forms.dll /reference:c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.Xml.dll /reference:"c:\Program Files\Reference Assemblies\Microsoft\Framework\v3.5\System.Xml.Linq.dll" /debug:pdbonly /filealign:512 /optimize+ /out:obj\Release\ConsoleApplication10.exe /target:exe Form1.cs Form1.Designer.cs Form2.cs Form2.Designer.cs Program.cs Properties\AssemblyInfo.cs

Compile complete -- 0 errors, 0 warnings
ConsoleApplication10 -> D:\SomePath\ConsoleApplication10.exe
echo D:\SomePath
D:\SomePath
========== Build: 1 succeeded or up-to-date, 0 failed, 0 skipped ==========

所以它似乎工作正常(即两个事件都已执行)。 不优雅,但可行。 这些宏应该允许您(需要一些技巧)访问项目项,例如 $(ProjectPath)Som​​eFile.xml

On C# Express the answer is simple: none.

C# Express does not support extensions. People have tried before and it has got ugly. Lawyers, mess, everything. The famous example is TestDriven.NET.

If you don't mind building at the command line, you could use an msbuild custom task, or a pre-build / post-build command.

In VS2005/VS2008 ("proper"), a "custom tool" is the one of the ways to go; this involves writing a Package; I've done this recently, and the code is open for inspection.

T4 is another option going forward. It didn't fit my needs, but it might do yours.


As a quick check of build events, I've added (in an express project) pre-build and post-build events (Project Properties -> Build Events) of:

echo $(TargetPath)

and

echo $(SolutionDir)

respectively; hit build, and the output is now:

------ Build started: Project: ConsoleApplication10, Configuration: Release Any CPU ------
echo D:\SomePath\ConsoleApplication10.exe
D:\SomePath\ConsoleApplication10.exe
c:\WINDOWS\Microsoft.NET\Framework\v3.5\Csc.exe /noconfig /unsafe+ /nowarn:1701,1702 /errorreport:prompt /warn:4 /define:TRACE /reference:"c:\Program Files\Reference Assemblies\Microsoft\Framework\v3.5\System.Core.dll" /reference:"c:\Program Files\Reference Assemblies\Microsoft\Framework\v3.5\System.Data.DataSetExtensions.dll" /reference:c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.Data.dll /reference:c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.Design.dll /reference:c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.dll /reference:c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.Drawing.dll /reference:c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.Windows.Forms.dll /reference:c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.Xml.dll /reference:"c:\Program Files\Reference Assemblies\Microsoft\Framework\v3.5\System.Xml.Linq.dll" /debug:pdbonly /filealign:512 /optimize+ /out:obj\Release\ConsoleApplication10.exe /target:exe Form1.cs Form1.Designer.cs Form2.cs Form2.Designer.cs Program.cs Properties\AssemblyInfo.cs

Compile complete -- 0 errors, 0 warnings
ConsoleApplication10 -> D:\SomePath\ConsoleApplication10.exe
echo D:\SomePath
D:\SomePath
========== Build: 1 succeeded or up-to-date, 0 failed, 0 skipped ==========

So it seems to work fine (i.e. both events have been executed). Not elegant, but workable. The macros should allow you (with some pokery) to access project items, for example $(ProjectPath)SomeFile.xml

请爱~陌生人 2024-08-05 18:44:19

您可以打开 csproj 文件(它只是一个 msbuild 脚本)并添加自定义目标。

为此,您可以覆盖两个默认目标:BeforeBuild 和 AfterBuild(可能更多,但我不确定其他目标)。

您还可以在新的 msbuild 任务中包含自定义 dll,但这会在打开项目时生成安全警告,除非您将 dll 添加到 msbuild 的 SafeImports 列表( HKLM\Software\Microsoft\Visual Studio\9.0\MSBuild\SafeImports ),如果是 8.0你用的是VS2005。

You can open the csproj file (its just an msbuild script) and add in custom targets as well.

There are two default targets provided for you to override for just such a purpose, BeforeBuild and AfterBuild (might be more but I'm unsure of the others).

You can also include custom dlls with new msbuild tasks, though this will generate a security warning when opening the project unless you add the dll to msbuild's SafeImports list ( HKLM\Software\Microsoft\Visual Studio\9.0\MSBuild\SafeImports ), 8.0 if you're using VS2005.

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