Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are there any examples of compiling CIL code from within a Visual Studio project

I realize that it's been asked and answered that Visual Studio does not support CIL/MSIL projects. The MSBuildContrib project has an ILASM task which allows you to compile IL files at build time.

Googled came up empty on examples for how to use this task.

Are there any examples of using ILASM as part of a Visual Studio solution? I realize that #develope and ILIDE support CIL... the objective here is to compile some CIL files as part of a Visual Studio solution.

like image 696
Mark Avatar asked Nov 01 '10 11:11

Mark


1 Answers

I gave Hans credit for answering this question, but after some experimentation I've got a solution which comes slightly closer to home:

HOW TO COMPILE CIL/MSIL INSIDE VISUAL STUDIO

The following hack gets you a project which:

  • Respects the Debug vs. Release build configuration in Visual Studio.
  • Optionally signs the resulting CIL assembly with the key file set up in the project preferences.
  • Can be added to source control.

Follow these steps:

  1. Create an empty C# classs library
  2. In the solution folder, right click the project and choose "unload project"
  3. Right click the unloaded project and choose "edit project"
  4. In the csprof file, scroll down to the bottom, and find the line which says "Import ... Project="$(MSBuildBinPath)\Microsoft.CSharp.targets", and replace it with the code at http://pastebin.com/KEJtyQLu (copied below)

Here's the 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>
like image 133
Mark Avatar answered Oct 06 '22 11:10

Mark