Suppose I have a class with 3 constructors, a default (no argument) constructor, a parameterized constructor, and a static constructor. like this:
public MyClass() { ... }
public MyClass(string arg) : this() { ... }
static MyClass() { ... }
Supposing I invoke the parameterized constructor, in what order do these constructors execute?
I thought it was static, then parameterized, then default. But ... my experience isn't agreeing with that.
The background: I have an app that embeds a referenced DLL as a resource. At runtime, the application registers an assembly resolver via
static MyClass()
{
AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(Resolver);
}
where the Resolver method is defined like this:
static System.Reflection.Assembly Resolver(object sender, ResolveEventArgs args)
{
....
}
I understood that the Resolver can produce an assembly any way it darn well chooses. In my app's case, it does a
Assembly.GetExecutingAssembly().GetManifestResourceStream(name);
where name is the name of the embedded resource. Then read all the bytes of this resource, and do an Assembly.Load(byte[]) on the block of bytes that is read.
This may sound strange to you, at first, but it works.
You might say, why in the world would you embed an assembly, rather than just ILMerge? Good question. I think I need to embed because the embedded assembly is signed, and I don't have the key to re-sign the merged assembly. So I embed.
The problem is this: suppose I declare a private instance member variable on the class, which is of a type that is defined in the embedded assembly. In my case, it's an enum, and I also initialize the value of that enum.
Now, if the static constructor has already run, then the initializer on that private member will have no problem running. But what I see is a "file not found" error - your basic Fusion error.
Unhandled Exception: System.IO.FileNotFoundException: Could not load file or assembly 'MyApp, Version=1.1.4.1, Culture=neutral, PublicKeyToken=edbe51ad942a3f5c' or one of its dependencies. The system cannot find the file specified.
File name: 'MyApp, Version=1.1.4.1, Culture=neutral, PublicKeyToken=edbe51ad942a3f5c'
WRN: Assembly binding logging is turned OFF.
To enable assembly bind failure logging, set the registry value[HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD) to 1.
Note: There is some performance penalty associated with assembly bind failure logging.
To turn this feature off, remove the registry value [HKLM\Software\Microsoft\Fusion!EnableLog].
If I remove the private instance variable, then I don't get the Fusion error.
I can use variables of that type, or any other type defined in the embedded assembly, later, as long as they are not initialized on as member instance variables in the class. I can use the types in a instance method, no problem.
Writing this up, I am thinking I may have come up with the answer to my own question. Maybe it is a JIT timing issue: maybe the instance constructors are being JIT'd before the static constructor is run. This would, maybe? lead to the Fusion error?
anyone have any insight?
It's not a huge crucial problem, because I can redesign the class to avoid the issue, removing all instance variables that depend on the embedded assembly. But I'd like to understand it.