Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何防止devenv在后台以静默方式调用编译目标?_C#_Visual Studio_Visual Studio 2012_Msbuild_Devenv - Fatal编程技术网

C# 如何防止devenv在后台以静默方式调用编译目标?

C# 如何防止devenv在后台以静默方式调用编译目标?,c#,visual-studio,visual-studio-2012,msbuild,devenv,C#,Visual Studio,Visual Studio 2012,Msbuild,Devenv,我注意到,当我在命令行上运行devenv时,它会在进行前台编译之前进行后台编译。我强烈怀疑我的环境有严重问题 请注意: PS C:\work\a> dir Directory: C:\work\a Mode LastWriteTime Length Name ---- ------------- ------ ---- d---- 11/26/2014 12:40 AM

我注意到,当我在命令行上运行devenv时,它会在进行前台编译之前进行后台编译。我强烈怀疑我的环境有严重问题

请注意:

PS C:\work\a> dir


    Directory: C:\work\a


Mode                LastWriteTime     Length Name
----                -------------     ------ ----
d----        11/26/2014  12:40 AM            .hg
-a---        11/25/2014  11:48 PM         87 .hgignore
-a---        11/26/2014  12:39 AM        264 1.ps1
-a---        11/26/2014  12:38 AM       1594 a.csproj
-a---        11/26/2014  12:43 AM          2 in.txt


PS C:\work\a>
在哪里

1.ps1

param([switch]$msbuild)

$in="in.txt"
(dir $in).LastWriteTime = Get-Date
dir $in |% { $_.Name + " : " + $_.LastWriteTime.ToString("yyyy-MM-dd HH:mm:ss.ffffff") }
if ($msbuild)
{
  msbuild .\a.csproj /v:m
}
else
{
  devenv .\a.csproj /build Debug /SafeMode
}
这是一个简单的Powershell脚本:

  • 触摸输入文件
  • 打印它的时间戳
  • 使用msbuild或命令行devenv生成项目
  • a.csproj

    <?xml version="1.0" encoding="utf-8"?>
    <Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">
      <PropertyGroup>
        <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
        <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
        <ProjectGuid>{EB5EB1C9-DC39-4E48-875B-094CBC0F468A}</ProjectGuid>
        <ProjectTypeGuids>{fae04ec0-301f-11d3-bf4b-00c04f79efbc}</ProjectTypeGuids>
        <OutputType>Library</OutputType>
        <AppDesignerFolder>Properties</AppDesignerFolder>
        <RootNamespace>a</RootNamespace>
        <AssemblyName>a</AssemblyName>
        <TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
        <FileAlignment>512</FileAlignment>
        <TargetFrameworkProfile />
        <OldToolsVersion>2.0</OldToolsVersion>
        <OutputPath>bin\$(Configuration)</OutputPath>
      </PropertyGroup>
      <ItemGroup>
        <None Include="in.txt" />
      </ItemGroup>
      <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
      <PropertyGroup>
        <CompileDependsOn>$(CompileDependsOn);MyTarget</CompileDependsOn>
      </PropertyGroup>
      <Target Name="A" BeforeTargets="_CheckForInvalidConfigurationAndPlatform" Condition="Exists('out.txt')">
        <ItemGroup>
          <Out Include="out.txt"/>
        </ItemGroup>
        <Message Text="%(Out.Identity) : %(Out.ModifiedTime)" Importance="High" />
      </Target>
      <Target Name="MyTarget" Inputs="in.txt" Outputs="out.txt">
        <Message Text="Touching now ..." Importance="High" />
        <Touch Files="out.txt" AlwaysCreate="true" />
      </Target>
    </Project>
    
    请注意,没有显示“正在触摸…”消息,但输出文件(2014-11-26 00:44:09.7744589)的时间戳已经比输入文件(2014-11-26 00:44:09.078676)的时间戳新,并且在运行第一个构建目标之前!事实上,当我构建诊断详细级别时,它告诉我输出相对于输入是最新的

    现在看看使用msbuild生成时会发生什么:

    PS C:\work\a> .\1.ps1 -msbuild
    in.txt : 2014-11-26 00:44:15.854564
    Microsoft (R) Build Engine version 4.0.30319.34209
    [Microsoft .NET Framework, version 4.0.30319.34209]
    Copyright (C) Microsoft Corporation. All rights reserved.
    
      out.txt : 2014-11-26 00:44:09.7744589
      Touching now ...
      a -> C:\work\a\bin\Debug\a.dll
    PS C:\work\a>
    
    一切正常-输出文件时间戳(2014-11-26 00:44:09.7744589)比输入文件时间戳(2014-11-26 00:44:15.854564)旧。因此,“现在触摸…”信息被打印出来

    注意,我在安全模式下运行devenv。我正在使用VS2012

    编辑1 连接到
    CoreBuildDependsOn
    会产生预期的结果:

    <CoreBuildDependsOn>$(CoreBuildDependsOn);MyTarget</CoreBuildDependsOn>
    
    请注意“正在触摸…”消息和生成之前的out.txt时间戳比in.txt的时间戳早-如预期的那样

    结论:devenv在后台静默调用
    Compile
    目标,而不是
    CoreBuild
    。为什么会这样?

    发现了这个-


    因此,答案是我们不能阻止后台编译,但是我们的自定义编译目标可以通过测试
    BuildingProject
    属性来防御性地编写。或者看起来是这样,因为我没有实际测试它,所以将逻辑移到CoreBuild依赖中对我来说已经足够了。

    也许VS需要进行后台编译以获取Intellisense信息?您可以尝试在.csproj文件中插入一个=false指令,看看这是否会产生您更喜欢的特性。试过了。没有任何影响。
    <CoreBuildDependsOn>$(CoreBuildDependsOn);MyTarget</CoreBuildDependsOn>
    
    PS C:\work\a> .\1.ps1
    in.txt : 2014-11-26 12:55:56.787793
    
    Microsoft (R) Microsoft Visual Studio 2012 Version 11.0.61030.0.
    Copyright (C) Microsoft Corp. All rights reserved.
    1>------ Build started: Project: a, Configuration: Debug Any CPU ------
    1>  out.txt : 2014-11-26 12:54:29.8173007
    1>  a -> C:\work\a\bin\Debug\a.dll
    1>  Touching now ...
    ========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========
    PS C:\work\a>