C# ILRepack throws "Object reference not set to an instance of an object."

88 Views Asked by At

Guess ILRepack is not used so much any more now when Single-file deployment exist.The last releases from il-repack and ILRepack.Lib.MSBuild.Task is quite old.

But I have a problem with ILRepack that I need help with. My code:

TestILRepack.csproj:

<Project Sdk="Microsoft.NET.Sdk">

    <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>net46</TargetFramework>
        <RuntimeIdentifier>win-x64</RuntimeIdentifier>
    </PropertyGroup>

    <ItemGroup>
        <PackageReference Include="CommandLineParser" Version="2.9.1" />
        <PackageReference Include="ILRepack.MSBuild.Task" Version="2.0.13" />
    </ItemGroup>

    <Target Name="ILRepack" BeforeTargets="PostBuildEvent" >
        <ILRepack Verbose="true" OutputType="$(OutputType)" MainAssembly="$(AssemblyName).exe" OutputAssembly="$(AssemblyName).exe" InputAssemblies="$(OutDir)\*.dll" WilcardInputAssemblies="true" WorkingDirectory="$(OutDir)" />
    </Target>

</Project>

Program.cs:

using CommandLine;
using System;

namespace CoolTool
{
  public class Program
  {
    public class Options
    {
      [Option('v', "verbose", Required = false, HelpText = "(Type: switch). Option to display detailed log.")]
      public bool Verbose { get; set; }
    }

    public static void Main(string[] args)
    {
      var parser = new Parser
      (
        settings =>
        {
          settings.CaseSensitive = false;
          settings.AutoHelp = true;
          settings.HelpWriter = Parser.Default.Settings.HelpWriter;
        }
      );

      parser.ParseArguments<Options>(args).WithParsed
      (
        options =>
        {
          if (options.Verbose)
          {
            Console.WriteLine("Verbose");
          }
          else
          {
            Console.WriteLine("Silent");
          }

          if (args.Length == 0)
          {
            Console.WriteLine("No arguments specified. Try \'--help\' command.");
          }
        }
      )
      .WithNotParsed(options => { });
    }
  }
}

I get exception:

Error       Object reference not set to an instance of an object.
at ILRepacking.Steps.Win32Resources.PE.ImageWriter.CopySection(Section from, Section to)
at ILRepacking.Steps.Win32Resources.Win32ResourceStep.Patch(String outFile)
at ILRepacking.ILRepack.Repack()
at ILRepack.MSBuild.Task.ILRepack.Execute() TestILRepack    C:\\Users\\hakan.olsson\\Desktop\\TestILRepack\\TestILRepack.csproj 19

I am using VS Enterprise 2022 (64-bit) Version 17.7.5

What am I doing wrong?

I Expected a nice single file executable

0

There are 0 best solutions below