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

Open
opened 2026-01-29 22:14:01 +00:00 by claunia · 0 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:01 +00:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/sharpcompress#577