In my program, I load a DLL using Assembly.LoadFrom(). The program has no Reference added to the DLL. It is loaded dynamically. The DLL has a class with a static constructor. I can see that it executes when the assembly is loaded. However, after a few hours, the static constructor executes again. There is only one place in the program from which the Assembly is loaded and this does not happen in a loop. This makes me think that the assembly has got Unloaded somehow and has automatically been reloaded. In what circumstances is this possible? Is there any other explanation to the static constructor being executed twice?
> In my program, I load a DLL using Assembly.LoadFrom(). The program has > no Reference added to the DLL. It is loaded dynamically. The DLL has a > class with a static constructor. I can see that it executes when the > assembly is loaded. However, after a few hours, the static constructor > executes again. There is only one place in the program from which the > Assembly is loaded and this does not happen in a loop. > This makes me think that the assembly has got Unloaded somehow and has > automatically been reloaded. In what circumstances is this possible? > Is there any other explanation to the static constructor being > executed twice?
> This is on .NET 3.0, C#
> Thanks, > Yash
Try displaying the stack trace in the static constructor to see why it might be called.
> In my program, I load a DLL using Assembly.LoadFrom(). The program has > no Reference added to the DLL. It is loaded dynamically. The DLL has a > class with a static constructor. I can see that it executes when the > assembly is loaded. However, after a few hours, the static constructor > executes again. There is only one place in the program from which the > Assembly is loaded and this does not happen in a loop. > This makes me think that the assembly has got Unloaded somehow and has > automatically been reloaded. In what circumstances is this possible? > Is there any other explanation to the static constructor being > executed twice?
> This is on .NET 3.0, C#
> Thanks, > Yash
Unless you loaded the assembly in a separate AppDomain, I don't think it will be unloaded until the App closes.
I'm not sure how instances of static classes are handled by the garbage collector, but perhaps your class is being collected somehow, so that the next time you access the static members, the static constructor is being executed again.