1

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?

Community
  • 1
  • 1
TheLethalCoder
  • 6,668
  • 6
  • 34
  • 69
  • Do you need to have the variable declaration within the `Programm` class? When you try to move the declaration and usage in another class and call it after you attached the event handler, it might work. – Toni Wenzel Jul 20 '17 at 11:01
  • @ToniWenzel For this particular case it needs to be inside this class. As a workaround I could make them all instance variables but I was trying to avoid that if possible. – TheLethalCoder Jul 20 '17 at 11:04

0 Answers0