views:

427

answers:

1

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.

+4  A: 

You are right, in terms of the order.

The static constructor runs first, then the non-parameterized constructor, then the parameterized constructor.

The JIT timing should not be an issue. The CLR guarantees that your static constructor will complete prior to any instance being constructed.

However, the assembly resolution is happening BEFORE your static constructor fires. The runtime needs to resolve the assembly (and its dependencies) prior to invoking any static construction. That's why you're running into this problem.

Reed Copsey
Yes, I can see that the assembly is being referenced before the static constructor fires. I think you're exactly right. The question is, WHY is that the case, *only when* an instance variable uses a type defined in the embedded assembly?
Cheeso
THe assembly needs to resolve in order to construct the type. When you go to access the type in the assembly, it needs to load that assembly, and potentially dependant assemblies (which calls assembly resolve), THEN it runs the static initializers, then static constructor on the type, then your instance constructors.
Reed Copsey
Yes, Reed - that's the issue. That explains what I observed.
Cheeso