<ItemGroup>
    <IL Include="**\*.il" Exclude="**\obj\**\*.il;**\bin\**\*.il" />
  </ItemGroup>
  <Target Name="ProcessILAfterCompile" AfterTargets="Compile">
    <CallTarget Targets="InitializeIL; CoreDecompile; CoreCompileIL" />
  </Target>
  <Target Name="InitializeIL">
    <PropertyGroup>
      <ILFile>@(IntermediateAssembly->'%(RootDir)%(Directory)%(Filename).il', ' ')</ILFile>
      <ILResourceFile>@(IntermediateAssembly->'%(RootDir)%(Directory)%(Filename).res', ' ')</ILResourceFile>
    </PropertyGroup>
  </Target>
  <Target Name="CoreDecompile" Inputs="@(IntermediateAssembly)" Outputs="$(ILFile)" Condition=" Exists ( @(IntermediateAssembly) ) ">
    <GetFrameworkSdkPath>
      <Output TaskParameter="Path" PropertyName="FrameworkSdkPath" />
    </GetFrameworkSdkPath>
    <PropertyGroup>
      <ILDasm>"$(FrameworkSdkPath)bin\ildasm.exe" /nobar /linenum /output:"$(ILFile)" @(IntermediateAssembly->'"%(FullPath)"', ' ')</ILDasm>
    </PropertyGroup>
    <PropertyGroup Condition=" Exists ( '$(FrameworkSdkPath)bin\NETFX 4.0 Tools\ildasm.exe' ) ">
      <ILDasm>"$(FrameworkSdkPath)bin\NETFX 4.0 Tools\ildasm.exe" /nobar /linenum /output:"$(ILFile)" @(IntermediateAssembly->'"%(FullPath)"', ' ')</ILDasm>
    </PropertyGroup>
    <PropertyGroup Condition=" Exists ( '$(FrameworkSdkPath)bin\NETFX 4.5.1 Tools\ildasm.exe' ) ">
      <ILDasm>"$(FrameworkSdkPath)bin\NETFX 4.5.1 Tools\ildasm.exe" /nobar /linenum /output:"$(ILFile)" @(IntermediateAssembly->'"%(FullPath)"', ' ')</ILDasm>
    </PropertyGroup>
    <PropertyGroup Condition=" Exists ( '$(FrameworkSdkPath)bin\NETFX 4.6 Tools\ildasm.exe' ) ">
      <ILDasm>"$(FrameworkSdkPath)bin\NETFX 4.6 Tools\ildasm.exe" /nobar /linenum /output:"$(ILFile)" @(IntermediateAssembly->'"%(FullPath)"', ' ')</ILDasm>
    </PropertyGroup>
    <PropertyGroup Condition=" Exists ( '$(FrameworkSdkPath)bin\NETFX 4.6.1 Tools\ildasm.exe' ) ">
      <ILDasm>"$(FrameworkSdkPath)bin\NETFX 4.6.1 Tools\ildasm.exe" /nobar /linenum /output:"$(ILFile)" @(IntermediateAssembly->'"%(FullPath)"', ' ')</ILDasm>
    </PropertyGroup>
    <PropertyGroup Condition=" Exists ( '$(FrameworkSdkPath)bin\NETFX 4.6.2 Tools\ildasm.exe' ) ">
      <ILDasm>"$(FrameworkSdkPath)bin\NETFX 4.6.2 Tools\ildasm.exe" /nobar /linenum /output:"$(ILFile)" @(IntermediateAssembly->'"%(FullPath)"', ' ')</ILDasm>
    </PropertyGroup>
    <Exec Command="$(ILDasm)" />
    <ItemGroup>
      <FileWrites Include="$(ILFile)" />
      <FileWrites Include="$(ILResourceFile)" />
    </ItemGroup>
    <PropertyGroup>
      <ILSource>$([System.IO.File]::ReadAllText($(ILFile)))</ILSource>
      <Replacement>// method ${method} forwardref removed for IL import</Replacement>
      <Pattern>\.method [^{}]+ cil managed forwardref[^}]+} // end of method (?&lt;method&gt;[^ \r\t\n]+)</Pattern>
      <ILSource>$([System.Text.RegularExpressions.Regex]::Replace($(ILSource), $(Pattern), $(Replacement)))</ILSource>
      <Pattern>\.method [^{}]+ cil managed[^\a]+"extern was not given a DllImport attribute"[^}]+} // end of method (?&lt;method&gt;[^ \r\t\n]+)</Pattern>
      <ILSource>$([System.Text.RegularExpressions.Regex]::Replace($(ILSource), $(Pattern), $(Replacement)))</ILSource>
    </PropertyGroup>
    <WriteLinesToFile File="$(ILFile)" Lines="$(ILSource)" Overwrite="true" />
    <PropertyGroup>
      <ILSource />
    </PropertyGroup>
    <Delete Files="@(IntermediateAssembly)" />
  </Target>
  <Target Name="CoreCompileIL" Inputs="@(IL)" Outputs="@(IntermediateAssembly)">
    <GetFrameworkPath>
      <Output TaskParameter="Path" PropertyName="FrameworkPath" />
    </GetFrameworkPath>
    <PropertyGroup>
      <ILAsm>"$(FrameworkPath)\ilasm.exe" /nologo /quiet /output:@(IntermediateAssembly->'"%(FullPath)"', ' ')</ILAsm>
    </PropertyGroup>
    <PropertyGroup Condition=" '$(FileAlignment)' != '' ">
      <ILAsm>$(ILAsm) /alignment=$(FileAlignment)</ILAsm>
    </PropertyGroup>
    <PropertyGroup Condition=" '$(BaseAddress)' != '' ">
      <ILAsm>$(ILAsm) /base=$(BaseAddress)</ILAsm>
    </PropertyGroup>
    <PropertyGroup Condition=" '$(OutputType)' == 'Library' ">
      <ILAsm>$(ILAsm) /dll</ILAsm>
    </PropertyGroup>
    <PropertyGroup Condition=" '$(DebugType)' == 'pdbonly' ">
      <ILAsm>$(ILAsm) /pdb</ILAsm>
    </PropertyGroup>
    <PropertyGroup Condition=" '$(DebugType)' == 'full' ">
      <ILAsm>$(ILAsm) /debug</ILAsm>
    </PropertyGroup>
    <PropertyGroup Condition=" '$(Optimize)' == 'true' ">
      <ILAsm>$(ILAsm) /optimize</ILAsm>
    </PropertyGroup>
    <PropertyGroup Condition=" '$(Platform)' == 'x64' ">
      <ILAsm>$(ILAsm) /pe64 /x64</ILAsm>
    </PropertyGroup>
    <PropertyGroup Condition=" '$(Platform)' == 'Itanium' ">
      <ILAsm>$(ILAsm) /pe64 /itanium</ILAsm>
    </PropertyGroup>
    <PropertyGroup Condition=" '$(AssemblyOriginatorKeyFile)' != '' ">
      <ILAsm>$(ILAsm) /key:"$(AssemblyOriginatorKeyFile)"</ILAsm>
    </PropertyGroup>
    <PropertyGroup Condition=" Exists ( '$(ILResourceFile)' ) ">
      <ILAsm>$(ILAsm) /resource:"$(ILResourceFile)"</ILAsm>
    </PropertyGroup>
    <PropertyGroup Condition=" Exists ( '$(ILFile)' ) ">
      <ILAsm>$(ILAsm) "$(ILFile)"</ILAsm>
    </PropertyGroup>
    <Exec Command="$(ILAsm) @(IL->'&quot;%(FullPath)&quot;', ' ')" />
    <ItemGroup>
      <FileWrites Include="@(IntermediateAssembly->'%(RootDir)%(Directory)DesignTimeResolveAssemblyReferencesInput.cache', ' ')" />
    </ItemGroup>
    <Touch Files="$(ILFile)" />
  </Target>