【问题标题】:Visual Studio Code Generation, project-based contentVisual Studio 代码生成,基于项目的内容
【发布时间】:2010-11-15 04:30:14
【问题描述】:

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

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

【问题讨论】:

    标签: visual-studio code-generation extensibility visual-studio-express


    【解决方案1】:

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

    C# Express 不支持扩展。人们以前尝试过,但它变得丑陋。律师,烂摊子,什么都有。著名的例子是TestDriven.NET

    如果您不介意在命令行构建,您可以使用 msbuild 自定义任务,或 pre-build / post-build 命令。

    在VS2005/VS2008(“适当的”)中,“自定义工具”是的方法之一;这涉及编写PackageI've done this recentlythe code 已开放供检查。

    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)SomeFile.xml

    【讨论】:

    • C# express 是否支持 pre/post 构建步骤?
    • 我刚刚检查过,似乎是;属性 -> 构建事件 > 构建前/构建后。当然,那你必须通过外部批处理/exe来做所有事情
    • 感谢您的回声。可能有用 - 请记住。
    【解决方案2】:

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

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

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

    【讨论】:

      猜你喜欢
      • 2011-04-02
      • 1970-01-01
      • 2010-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多