When I create a new AppDomain using AppDomain.CreateDomain
in C#, will static constructors be called as asseblies are loaded inside the newly created AppDomain?
The assemblies in question have already been loaded into the current domain.
When I create a new AppDomain using AppDomain.CreateDomain
in C#, will static constructors be called as asseblies are loaded inside the newly created AppDomain?
The assemblies in question have already been loaded into the current domain.
No - static constructors will only be called the first time a static member is accessed or an instance is created.
The static constructor will be invoked once per AppDomain
though, if that's what you were concerned about. It's not like having executed once in a different AppDomain
, the types in the new AppDomain
get left uninitialized :)
Note that type initializers for types without static constructors may be executed earlier or later than those for types with static constructors, and the precise implementation details have changed for .NET 4.
Check this site: http://codeidol.com/csharp/net-framework/Threads,-AppDomains,-and-Processes/AppDomains/
Here is an excerpt:
Unless you use something like thread-static fields, each AppDomain contains a copy of all static fields. All class (or static) constructors will run once within a given AppDomain. This means that if you load the same assembly in different AppDomains, each will run the class constructors, and each will contain separate values for all static fields, for example.