Attaching to AssemblyResolve earlier

160 Views Asked by At

Consider the following code inside of the Program class:

static EmbeddedClass MyClass;

static Program()
{
     AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
}

static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
    //Load EmbeddedClass here
}

For this application we need the assembly EmbeddedClass is inside of to be embedded inside the application and not alongside it as a dll. So too load the assembly we need to use AssemblyResolve.

However, the above code will not work because upon running it gives:

An unhandled exception of type 'System.TypeInitializationException' occurred in mscorlib.dll

Additional information: The type initializer for 'Program' threw an exception.

I believe this is because it is trying to load the EmbeddedClass's assembly before it runs the static constructor.

Is there anyway to solve this situation?

0

There are 0 best solutions below