Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ModuleInitializerAttribute loading #21164

Closed
curia-damiano opened this issue Oct 20, 2020 · 1 comment · Fixed by #21419
Closed

ModuleInitializerAttribute loading #21164

curia-damiano opened this issue Oct 20, 2020 · 1 comment · Fixed by #21419
Assignees
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

Comments

@curia-damiano
Copy link

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.

@BillWagner
Copy link
Member

Thanks for writing this @curia-damiano

We'll get this updated to correctly reflect the behavior.

@BillWagner BillWagner added P1 and removed ⌚ Not Triaged Not triaged labels Oct 21, 2020
@BillWagner BillWagner added 🗃️ Technology - C# What's New 🏁 Release: .NET 5 Work items for the .NET 5 release doc-bug Problem with the content; needs to be fixed [org][type][category] labels Oct 21, 2020
@BillWagner BillWagner self-assigned this Nov 4, 2020
BillWagner added a commit to BillWagner/docs that referenced this issue Nov 9, 2020
The description of "when an assembly loads" was misleading. The guarantee is similar to a static constructor, but must run before any static constructors in that module.

Fixes dotnet#21164
BillWagner added a commit that referenced this issue Nov 10, 2020
The description of "when an assembly loads" was misleading. The guarantee is similar to a static constructor, but must run before any static constructors in that module.

Fixes #21164
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
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
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants