ModuleInitializerAttribute loading #21164
Labels
🏁 Release: .NET 5
Work items for the .NET 5 release
doc-bug
Problem with the content; needs to be fixed [org][type][category]
dotnet-csharp/svc
whats-new/subsvc
The following sentence at the end of the page is not correct:
The second new feature for code generators is module initializers. Module initializers are methods that have the ModuleInitializerAttribute attribute attached to them. These methods will be called by the runtime when the assembly loads.
I have developed a sample POC with the following code:
var assembly = Assembly.LoadFrom(@"demo.dll"); var type = assembly.GetType("SampleNamespace.SampleClass"); var constructorInfo = type.GetConstructor(new Type[] { }); _ = constructorInfo.Invoke(null);
The module initializer is NOT invoked the the assembly is loaded with Assembly.LoadFrom, but only before the last line, when I create an instance of my class.
In fact, in dotnet/runtime#35749, it is documented correctly that "This is a special static constructor that has additional runtime guarantees. It will execute before any other field access or method invocation within the entire module."
Document Details
⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.
The text was updated successfully, but these errors were encountered: