[PR #716] [MERGED] replace Activator.CreateInstance to Func for avoiding error in NativeAOT #1176

Closed
opened 2026-01-29 22:19:21 +00:00 by claunia · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/adamhathcock/sharpcompress/pull/716
Author: @itn3000
Created: 12/21/2022
Status: Merged
Merged: 12/21/2022
Merged by: @adamhathcock

Base: masterHead: fix-reflection-aot-error


📝 Commits (3)

  • a7a5c41 replace Activator.CreateInstance to Func for avoiding error in NativeAOT
  • 61bfbdb fix compilation error
  • 7842168 remove redundant cast

📊 Changes

1 file changed (+10 additions, -10 deletions)

View changed files

📝 src/SharpCompress/Compressors/Xz/Filters/BlockFilter.cs (+10 -10)

📄 Description

Overview

This fixes System.MissingMethodException when using XZ decompressor in NativeAOT.

Steps of reproduce

  1. create new console project and set TargetFramework to net7.0
  2. add SharpCompress package(this issue was investigated in 0.32.2)
  3. edit cs to following reproducible code
  4. publish with NativeAOT(and rid) option
  5. create xz archive file
  6. execute published executable file

Repro code

using SharpCompress.Compressors.Xz;

using(var istm = File.OpenRead("test.xz"))
using(var xstm = new XZStream(istm))
using(var ostm = File.Create("test.txt"))
{
    xstm.CopyTo(ostm);
}

Expected

"test.txt" is created and written decompressed result

Actual

System.MissingMethodException is thrown.

Unhandled Exception: System.MissingMethodException: No parameterless constructor defined for type 'SharpCompress.Compressors.Xz.Filters.Lzma2Filter'.
   at System.ActivatorImplementation.CreateInstance(Type, Boolean) + 0x208
   at System.Reflection.Runtime.General.ReflectionCoreCallbacksImplementation.ActivatorCreateInstance(Type, Boolean) + 0x2c
   at System.Activator.CreateInstance(Type, Boolean) + 0x48
   at System.Activator.CreateInstance(Type) + 0x20
   at SharpCompress.Compressors.Xz.Filters.BlockFilter.Read(BinaryReader) + 0x147
   at SharpCompress.Compressors.Xz.XZBlock.ReadFilters(BinaryReader, Int64) + 0x64
   at SharpCompress.Compressors.Xz.XZBlock.LoadHeader() + 0xdd
   at SharpCompress.Compressors.Xz.XZBlock.Read(Byte[], Int32, Int32) + 0x3f
   at SharpCompress.Compressors.Xz.XZStream.ReadBlocks(Byte[], Int32, Int32) + 0x89
   at SharpCompress.Compressors.Xz.XZStream.Read(Byte[], Int32, Int32) + 0x69
   at System.IO.Stream.CopyTo(Stream, Int32) + 0x155
   at System.IO.Stream.CopyTo(Stream) + 0x58
   at Program.<Main>$(String[]) + 0x82
   at xzcomptest!<BaseAddress>+0x2b9c57
   at xzcomptest!<BaseAddress>+0x2b9cea

Workaround

Currently, this can be avoided by adding class under SharpCompress.Compressors.Xz.Filters in rd.xml.
here is example of rd.xml

<Directives xmlns="http://schemas.microsoft.com/netfx/2013/01/metadata">
    <Application>
        <Assembly Name="SharpCompress">
            <Type Name="SharpCompress.Compressors.Xz.Filters.Lzma2Filter" Dynamic="Required All"/>
        </Assembly>
    </Application>
</Directives>

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/adamhathcock/sharpcompress/pull/716 **Author:** [@itn3000](https://github.com/itn3000) **Created:** 12/21/2022 **Status:** ✅ Merged **Merged:** 12/21/2022 **Merged by:** [@adamhathcock](https://github.com/adamhathcock) **Base:** `master` ← **Head:** `fix-reflection-aot-error` --- ### 📝 Commits (3) - [`a7a5c41`](https://github.com/adamhathcock/sharpcompress/commit/a7a5c413709fbeec823ec1f6618f9cc67f1356a2) replace Activator.CreateInstance to Func for avoiding error in NativeAOT - [`61bfbdb`](https://github.com/adamhathcock/sharpcompress/commit/61bfbdb26efb40e9c87eb64981e5259e14adee25) fix compilation error - [`7842168`](https://github.com/adamhathcock/sharpcompress/commit/78421683fec02345d71a4143e1fd09b13dd23e68) remove redundant cast ### 📊 Changes **1 file changed** (+10 additions, -10 deletions) <details> <summary>View changed files</summary> 📝 `src/SharpCompress/Compressors/Xz/Filters/BlockFilter.cs` (+10 -10) </details> ### 📄 Description # Overview This fixes `System.MissingMethodException` when using XZ decompressor in NativeAOT. ## Steps of reproduce 1. create new console project and set TargetFramework to net7.0 2. add SharpCompress package(this issue was investigated in 0.32.2) 3. edit cs to following reproducible code 4. publish with NativeAOT(and rid) option 5. create xz archive file 6. execute published executable file ### Repro code ```csharp using SharpCompress.Compressors.Xz; using(var istm = File.OpenRead("test.xz")) using(var xstm = new XZStream(istm)) using(var ostm = File.Create("test.txt")) { xstm.CopyTo(ostm); } ``` ## Expected "test.txt" is created and written decompressed result ## Actual System.MissingMethodException is thrown. ``` Unhandled Exception: System.MissingMethodException: No parameterless constructor defined for type 'SharpCompress.Compressors.Xz.Filters.Lzma2Filter'. at System.ActivatorImplementation.CreateInstance(Type, Boolean) + 0x208 at System.Reflection.Runtime.General.ReflectionCoreCallbacksImplementation.ActivatorCreateInstance(Type, Boolean) + 0x2c at System.Activator.CreateInstance(Type, Boolean) + 0x48 at System.Activator.CreateInstance(Type) + 0x20 at SharpCompress.Compressors.Xz.Filters.BlockFilter.Read(BinaryReader) + 0x147 at SharpCompress.Compressors.Xz.XZBlock.ReadFilters(BinaryReader, Int64) + 0x64 at SharpCompress.Compressors.Xz.XZBlock.LoadHeader() + 0xdd at SharpCompress.Compressors.Xz.XZBlock.Read(Byte[], Int32, Int32) + 0x3f at SharpCompress.Compressors.Xz.XZStream.ReadBlocks(Byte[], Int32, Int32) + 0x89 at SharpCompress.Compressors.Xz.XZStream.Read(Byte[], Int32, Int32) + 0x69 at System.IO.Stream.CopyTo(Stream, Int32) + 0x155 at System.IO.Stream.CopyTo(Stream) + 0x58 at Program.<Main>$(String[]) + 0x82 at xzcomptest!<BaseAddress>+0x2b9c57 at xzcomptest!<BaseAddress>+0x2b9cea ``` ## Workaround Currently, this can be avoided by adding class under `SharpCompress.Compressors.Xz.Filters` in rd.xml. here is example of [rd.xml](https://github.com/kant2002/RdXmlLibrary) ```xml <Directives xmlns="http://schemas.microsoft.com/netfx/2013/01/metadata"> <Application> <Assembly Name="SharpCompress"> <Type Name="SharpCompress.Compressors.Xz.Filters.Lzma2Filter" Dynamic="Required All"/> </Assembly> </Application> </Directives> ``` --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
claunia added the pull-request label 2026-01-29 22:19:21 +00:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/sharpcompress#1176