Module Initializers in C#

Module initializers in C#

After eleven years, this is finally supported by the language in C#9

https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-9#support-for-code-generators

Incorrect behavior of the [ModuleInitializer]?

Here is an explanation on github

A module initializer is executed at, or sometime before, first access
to any static field or first invocation of any method defined in the
module.

How can I run a static initializer method in C# before the Main() method?

Simply do the initialization inside a static constructor for Foo.

From the documentation:

A static constructor is called automatically to initialize the class
before the first instance is created or any static members are
referenced.

Static constructor for the whole assembly

Check Module initializers in C#.

Automatically run code on referenced assembly during startup?? What is this called?

Turns out I was looking for PreApplicationStartMethod! Thanks all!



Related Topics



Leave a reply



Submit