Usage - ILStrip

Code Example

In the reasons of convenience, all operations are divided into separate methods, that should be called in sequence:

ILStrip stripper =
  new ILStrip(inputPath), 
// new ILStrip(inputStream), 
// new ILStrip(assemblyDefinition), 
    
// add whatever you want by name.
// executable's Main(string[] args) will be added automatically
stripper.EntryPoints.Add("MyNamespace.MyClass"), 
stripper.EntryPointBamls.Add("ui/mywindow.baml"), 
    
// walk the assembly opcodes to find all types and references used
stripper.ScanUsedClasses(), 

// walk the assembly types to build list of all types that aren't used
stripper.ScanUnusedClasses(), 

// remove all unused types
stripper.CleanupUnusedClasses(), 

// exclude necessary resources from cleanup
stripper.UnusedResourceExclusions.Add("MyNamespace.MyImage.png"), 

// exclude necessary WPF resources from cleanup
stripper.UnusedWpfResourceExclusions.Add("res/myimage.png"), 

// remove all unused WinForms and WPF resources
stripper.CleanupUnusedResources(), 

// remove all unused references
stripper.CleanupUnusedReferences(), 

// exclude the target public API
stripper.MakeInternalExclusions.Add("MyNamespace.MyClass"), 

// hide all other with internal
stripper.MakeInternal(), 

stripper.Save(outputPath), 
// stripper.Save(outputStream), 

Commandline Tool

In reasons of convenience there is a commandline tool, built from improvised means. This makes the ILStrip to work as standalone or as a part of any build script. Usage:

Syntax:

BrokenEvent.ILStrip.CLI.exe input output [-s] [-e MyNamespace.MyClass] [-h]
[-he MyNamespace.MyClass] [-u] [-re MyNamespace.MyResource]
[-we resources/myresource.png] [-import myApp.exe]

Arguments:

input           Input assembly filename to process. Required.
output          Output assembly filename to save processed assembly. Required.
-s, -silent     Suppresses logging. Optional.
-e              User defined entry point classes list to start analysis. Multiple values. Optional.
-h, -hide       Hide public API with internal access modifier. Optional.
-he             Exclusions for -h option. Multiple values. Optional.
-u              Removes all unknown resources. Optional.
-re             Resource exclusions for -u option. Multiple values. Optional.
-we             WPF Resource exclusions for -u option. Multiple values. Optional.
-import         List of assemblies which use current to import used types as entry points. Optional.

MSBuild Task of NuGet Package

This information applies only to BrokenEvent.ILStrip.CLI package.

The package will install MSBuild target when installed by the NuGet. By default this target allows to strip the target binary to optimize its filesize.

If you use ILRepack, the target binary filename may be changed, so the ILStrip's target couldn't find target file and will fail.

To control the target, you may use the following MSBuild properties:

PropertyGroups

  • ILStripInternalize - whether to hide public types of stripped binary with internal. Boolean value (True/False). Feature is disabled by default.
  • ILStripSrc - name of source file to process. By default it is assembly name + default extension.
  • ILStripDst - name of destionation file to to write assembly after processing. By default it is assembly name + default extension.

ItemGroups

  • ILStripEntryPoints - list of custom entry points to start scanning from. For xecutables the Program class will be added by default. Format is Namespace.ClassName. Example: WindowsFormsApp5.Program.
  • ILStripEntryPointBAMLs - list of custom entry point BAMLs. When you load .xaml by filename this couldn't be detected with IL scanning, so you should use this ItemGroup in such cases.
  • ILStripHideExclusions - list of types to exclude from hiding with internal. Meaningless if ILStripInternalize not set to True.
  • ILStripImport - list of filenames of assemblies to import types used by them as entry points.

Properties

The most simple way to add custom properties to MSBuild of Visual Studio project is to create a file named Directory.Build.props at root of project.

Example of such file:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <ILStripInternalize>True</ILStripInternalize>
    <ILStripSrc>WindowsFormsApp5.exe</ILStripSrc>
    <ILStripDst>WindowsFormsApp1.exe</ILStripDst>
  </PropertyGroup>

  <ItemGroup>
    <ILStripEntryPoints Include="WindowsFormsApp5.Program"/>
    <ILStripEntryPoints Include="WindowsFormsApp5.Class1"/>
  </ItemGroup>
  <ItemGroup>
    <ILStripEntryPointBAMLs Include="ui/mainwindow.baml"/>
  </ItemGroup>
  <ItemGroup>
    <ILStripHideExclusions Include="WindowsFormsApp5.Class1"/>
  </ItemGroup>
  <ItemGroup>
    <ILStripImport Include="mylib.dll"/>
  </ItemGroup>
</Project>