-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
New API: ModuleInitializerAttribute #35749
Comments
I couldn't figure out the best area label to add to this issue. Please help me learn by adding exactly one area label. |
@GrabYourPitchforks what does the "blocking" label signify here? |
It puts this issue to the front of the |
@jaredpar presumably this feature won't have language syntax. Normally we don't use namespace System.Runtime.CompilerServices
{
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
public sealed class ModuleInitializerAttribute : Attribute
{
}
} |
What are next steps? Are we ready to open a PR to dotnet/runtime to add the API? |
Yeah this is meant for really advanced users, not main stream use hence this namespace feels fine to me. |
That is probably the easiest solution. You could ask @danmosemsft for a dev but attributes are basically trivial. |
You can use #454 as a template. |
We'd planned on having @RikkiGibson do the work here so we'll get a PR out soon-ish for this. |
The module initializers feature for C# 9 enables a new attribute,
[ModuleInitializer]
, to be applied to any number of static methods in a compilation. The marked methods are guaranteed to run before any other method is invoked or field is accessed in the whole module.Usage example:
API proposal
Compiler behavior
Module initializer methods (methods marked with
[ModuleInitializer]
) must be:If any methods match these criteria, a module initializer (runtime term for
<Module>..cctor
) is emitted. 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. The emitted body is a series ofcall
instructions, one to each module initializer method in a deterministic but undefined order, followed by aret
instruction.The VB strategy has not been determined. It would either behave the same way or error when System.Runtime.CompilerServices.ModuleInitializerAttribute is applied to a method.
References
Language proposal (credit to @RikkiGibson) with link to meeting notes:
https://github.com/dotnet/csharplang/blob/master/proposals/module-initializers.md
Compiler feature doc (credit to @RikkiGibson):
https://github.com/dotnet/roslyn/blob/mi-feature-doc/docs/features/module-initializers.md
Runtime spec: https://github.com/dotnet/runtime/blob/master/docs/design/specs/Ecma-335-Augments.md#module-initializer
Test plan umbrella: dotnet/roslyn#40500
Implementation underway: https://github.com/dotnet/roslyn/commits/features/module-initializers
The text was updated successfully, but these errors were encountered: