Unable to use Assembly.LoadFrom to call functions in a class library that depends on SharpCompress. #579

Closed
opened 2026-01-29 22:14:06 +00:00 by claunia · 3 comments
Owner

Originally created by @KaGaMiLinAlfa on GitHub (Jul 18, 2023).

I have two programs, one is a console application called ConsoleApp, and the other is a class library called ClassLibrary. Both of them are built using Core 3.1.

ConsoleApp does not reference SharpCompress, while ClassLibrary references and uses SharpCompress 33.0.

In ConsoleApp, I use Assembly.LoadFrom to load ClassLibrary and invoke related functions. However, when calling these functions
I encounter an error with the following message: 'The type initializer for 'SharpCompress.Common.ArchiveEncoding' threw an exception.'

If I add a reference to SharpCompress in ConsoleApp, the error does not occur. Why does this happen?
I want ConsoleApp to be able to call the functions in ClassLibrary without referencing SharpCompress. Is there any solution available?

I have been researching this issue for quite some time but haven't found a solution. I would appreciate any help. Thank you!

Here is the code I'm using for debugging purposes:

ConsoleApp.cs:

static void Main(string[] args)
{
    var assembly = Assembly.LoadFrom("*****\\ClassLibrary1\\bin\\Release\\netcoreapp3.1\\publish\\ClassLibrary1.dll");
    var type = assembly.GetType("ClassLibrary1.Class1");
    object obj = Activator.CreateInstance(type);
    MethodInfo method = type.GetMethod("Run");
    method.Invoke(obj, null);
}

ClassLibrary.Class1:

public void Run()
{
    try
    {
        var ArchiveEncoding = new ArchiveEncoding();
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }
}
Originally created by @KaGaMiLinAlfa on GitHub (Jul 18, 2023). I have two programs, one is a console application called ConsoleApp, and the other is a class library called ClassLibrary. Both of them are built using Core 3.1. ConsoleApp does not reference SharpCompress, while ClassLibrary references and uses SharpCompress 33.0. In ConsoleApp, I use Assembly.LoadFrom to load ClassLibrary and invoke related functions. However, when calling these functions I encounter an error with the following message: 'The type initializer for 'SharpCompress.Common.ArchiveEncoding' threw an exception.' If I add a reference to SharpCompress in ConsoleApp, the error does not occur. Why does this happen? I want ConsoleApp to be able to call the functions in ClassLibrary without referencing SharpCompress. Is there any solution available? I have been researching this issue for quite some time but haven't found a solution. I would appreciate any help. Thank you! Here is the code I'm using for debugging purposes: ConsoleApp.cs: ``` static void Main(string[] args) { var assembly = Assembly.LoadFrom("*****\\ClassLibrary1\\bin\\Release\\netcoreapp3.1\\publish\\ClassLibrary1.dll"); var type = assembly.GetType("ClassLibrary1.Class1"); object obj = Activator.CreateInstance(type); MethodInfo method = type.GetMethod("Run"); method.Invoke(obj, null); } ``` ClassLibrary.Class1: ``` public void Run() { try { var ArchiveEncoding = new ArchiveEncoding(); } catch (Exception ex) { Console.WriteLine(ex.Message); } } ```
claunia added the questionup for grabs labels 2026-01-29 22:14:06 +00:00
Author
Owner

@Erior commented on GitHub (Sep 4, 2023):

Have you tried Assembly.LoadFrom and dependencies ?

@Erior commented on GitHub (Sep 4, 2023): Have you tried [Assembly.LoadFrom and dependencies](https://stackoverflow.com/questions/24956949/assembly-loadfrom-and-dependencies) ?
Author
Owner

@KaGaMiLinAlfa commented on GitHub (Sep 15, 2023):

Have you tried Assembly.LoadFrom and dependencies ?

Thank you for your suggestion.

I have attempted to use AssemblyResolve While I successfully loaded SharpCompress.dll, it also requires the System.Text.Encoding.CodePages.dll

However, when trying to load System.Text.Encoding.CodePages.dll, I encountered the following error:
'System.Text.Encoding.CodePages, Version=7.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' could not be loaded.

I can ensure that the necessary DLL files are all present. Nevertheless, I still encountered the same issue:

The type initializer for 'SharpCompress.Common.ArchiveEncoding' threw an exception.
Unhandled exception.System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
 --->System.TypeInitializationException: The type initializer for 'SharpCompress.Common.ArchiveEncoding' threw an exception.
 --->System.IO.FileLoadException: Could not load file or assembly 'System.Text.Encoding.CodePages, Version=7.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.Could not find or load a specific file. (0x80131621)
File name: 'System.Text.Encoding.CodePages, Version=7.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
 --->System.IO.FileLoadException: Could not load file or assembly 'System.Text.Encoding.CodePages, Version=7.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.

I have created a repository to test this issue, hoping to provide more specific information to you.
https://github.com/KaGaMiLinAlfa/SharpCompressLoadProblem.git

@KaGaMiLinAlfa commented on GitHub (Sep 15, 2023): > Have you tried [Assembly.LoadFrom and dependencies](https://stackoverflow.com/questions/24956949/assembly-loadfrom-and-dependencies) ? Thank you for your suggestion. I have attempted to use AssemblyResolve While I successfully loaded SharpCompress.dll, it also requires the System.Text.Encoding.CodePages.dll However, when trying to load System.Text.Encoding.CodePages.dll, I encountered the following error: `'System.Text.Encoding.CodePages, Version=7.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' could not be loaded.` I can ensure that the necessary DLL files are all present. Nevertheless, I still encountered the same issue: ``` The type initializer for 'SharpCompress.Common.ArchiveEncoding' threw an exception. Unhandled exception.System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. --->System.TypeInitializationException: The type initializer for 'SharpCompress.Common.ArchiveEncoding' threw an exception. --->System.IO.FileLoadException: Could not load file or assembly 'System.Text.Encoding.CodePages, Version=7.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.Could not find or load a specific file. (0x80131621) File name: 'System.Text.Encoding.CodePages, Version=7.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' --->System.IO.FileLoadException: Could not load file or assembly 'System.Text.Encoding.CodePages, Version=7.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. ``` I have created a repository to test this issue, hoping to provide more specific information to you. `https://github.com/KaGaMiLinAlfa/SharpCompressLoadProblem.git`
Author
Owner

@KaGaMiLinAlfa commented on GitHub (Jan 2, 2024):

In fact, SharpCompress did not provide support for 3.1, and I have been using Core 3.1 all along
After switching to versions 6.0 or 7.0, which clearly indicate support, it started working correctly

Thank you @Erior for telling me this answer

@KaGaMiLinAlfa commented on GitHub (Jan 2, 2024): In fact, SharpCompress did not provide support for 3.1, and I have been using Core 3.1 all along After switching to versions 6.0 or 7.0, which clearly indicate support, it started working correctly Thank you @Erior for telling me this answer
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/sharpcompress#579