代码之家  ›  专栏  ›  技术社区  ›  Mark

有没有在Visual Studio项目中编译CIL代码的示例?

  •  5
  • Mark  · 技术社区  · 13 年前

    我意识到有人问我并回答说Visual Studio不支持CIL/MSIL项目。这个 MSBuildContrib 项目有一个ILASM任务,允许您在构建时编译IL文件。

    关于如何使用这个任务的例子,google给出了一些空的例子。

    有没有使用ILASM作为Visual Studio解决方案的一部分的例子? 我意识到开发和ILide支持CIL…这里的目标是编译一些CIL文件 作为Visual Studio解决方案的一部分 .

    2 回复  |  直到 11 年前
        1
  •  1
  •   Hans Passant    13 年前

    您可以添加一个“makefile项目”。模板是VisualC++的,一般的。此项目类型允许您为生成、清除和重新生成操作指定自定义生成命令。环境将自动设置,这样您就不必乱弄路径了。C++ IDE还支持创建自定义生成规则,这样您就可以从文件扩展名(.IL)中自动触发正确的工具(IASM.exe)。

    不过,这差不多是最好的了。项目中没有多个.il文件自动映射到单个生成命令。为此,需要编写nmake.exe makefile。还要注意,自定义构建规则支持在VS2010中被破坏。

        2
  •  13
  •   Suzanne Soy    11 年前

    我承认汉斯回答了这个问题,但经过一些试验,我找到了一个更接近家的解决方案:

    如何在Visual Studio中编译CIL/MSIL

    以下黑客程序为您提供了一个项目:

    • 尊重Visual Studio中的调试与发布生成配置。
    • 也可以使用项目首选项中设置的密钥文件对生成的CIL程序集进行签名。
    • 可以添加到源代码管理。

    遵循以下步骤:

    1. 创建一个空的C类库
    2. 在解决方案文件夹中,右键单击项目并选择“卸载项目”
    3. 右键单击卸载的项目并选择“编辑项目”
    4. 在csprof文件中,向下滚动到底部,找到“import…project=“$(msbuildbinpath)\microsoft.csharp.targets”,并将其替换为位于的代码 http://pastebin.com/KEJtyQLu (以下复制)

    以下是XML:

      <Import Project="$(MSBuildToolsPath)\Microsoft.Common.targets" />
    
      <Target Name="CreateManifestResourceNames" />
    
      <Target Name="CoreCompile" Inputs="$(MSBuildAllProjects);@(Compile);" Outputs="@(IntermediateAssembly);$(NonExistentFile);">
        <GetFrameworkPath>
          <Output TaskParameter="Path" PropertyName="FrameworkPath" />
        </GetFrameworkPath>
    
        <PropertyGroup>
          <IlAsmCommand>&quot;$(FrameworkPath)\Ilasm.exe&quot; /NOLOGO /DLL /OUTPUT:&quot;@(IntermediateAssembly)&quot; </IlAsmCommand>
        </PropertyGroup>
    
        <PropertyGroup Condition=" '$(Configuration)' == 'Debug' " >
          <IlAsmCommand>$(IlAsmCommand) /DEBUG </IlAsmCommand>
        </PropertyGroup>
    
        <PropertyGroup Condition=" '$(Configuration)' == 'Release' " ><IlAsmCommand>$(IlAsmCommand) /OPTIMIZE </IlAsmCommand></PropertyGroup>
    
        <PropertyGroup Condition=" '$(AssemblyOriginatorKeyFile)' != '' " >
          <IlAsmCommand>$(IlAsmCommand) /KEY:&quot;$(AssemblyOriginatorKeyFile)&quot; </IlAsmCommand>
        </PropertyGroup>
    
        <Exec Command="$(IlAsmCommand) @(Compile->'&quot;%(FullPath)&quot;', ' ')" 
              Outputs="@(IntermediateAssembly)" />
    
        <CallTarget Targets="$(TargetsTriggeredByCompilation)" Condition="'$(TargetsTriggeredByCompilation)' != ''" />
    
      </Target>