Champion: Module Initializers (VS 16.8, .NET 5)
See also https://github.com/dotnet/csharplang/issues/2486 Although the .NET platform has a feature that directly supports writing initialization code for the assembly (technically, the module), it is not exposed in C#. This is a rather niche scenario, but once you run into it the solutions appear to be pretty painful. I have seen reports of [a number of customers](https://www.google.com/search?q=.net+module+constructor+c%23&oq=.net+module+constructor) (inside and outside Microsoft) struggle with the problem, and there are no doubt more undocumented cases. I suggest that we would add a tiny feature to support this without any explicit syntax, by having the C# compiler recognize a module attribute with a well-known name, like the following: ``` c# namespace System.Runtime.CompilerServices { [Obsolete("This attribute is only to be used in C# language version 9.0 or later", true)] [AttributeUsage(AttributeTargets.Module, AllowMultiple = false)] public class ModuleInitializerAttribute : Attribute { public ModuleInitializerAttribute(Type type) { } } } ``` You would use it like this ``` c# [module: System.Runtime.CompilerServices.ModuleInitializerAttribute(typeof(MyModuleInitializer))] internal static class MyModuleInitializer { static MyModuleInitializer() { // put your module initializer here } } ``` and the C# compiler would then emit a module constructor that causes the static constructor of the identified type to be trigg
Posted by gafter on Jun 18, 2019, 9:28 PM
Tags
Related issues
- Add 65536 size support for maps
- TypeScript 7.1 Iteration Plan
- nvim-treeconfig: upstream nvim-treesitter
- Add the "action" and "method" attributes to <button>
- [FEATURE] Support multiple Connector accounts (same connector, different accounts) in Claude and Claude Code on the web (claude.ai/code)
- > We were imposed GitHub for so many exercises!
Raise real-world problems, find people facing the same issue, and discuss solutions.3,500 issues0 comments
support (Software)
Add 65536 size support for maps
TypeScript 7.1 Iteration Plan
nvim-treeconfig: upstream nvim-treesitter
Add the "action" and "method" attributes to <button>
[FEATURE] Support multiple Connector accounts (same connector, different accounts) in Claude and Claude Code on the web (claude.ai/code)
> We were imposed GitHub for so many exercises!
Add support for Visual Studio 2026 Integration
Support for Helm v4
Champion: Module Initializers (VS 16.8, .NET 5)
See also https://github.com/dotnet/csharplang/issues/2486 Although the .NET platform has a feature that directly supports writing initialization code for the assembly (technically, the module), it is not exposed in C#. This is a rather niche scenario, but once you run into it the solutions appear to be pretty painful. I have seen reports of [a number of customers](https://www.google.com/search?q=.net+module+constructor+c%23&oq=.net+module+constructor) (inside and outside Microsoft) struggle with the problem, and there are no doubt more undocumented cases. I suggest that we would add a tiny feature to support this without any explicit syntax, by having the C# compiler recognize a module attribute with a well-known name, like the following: ``` c# namespace System.Runtime.CompilerServices { [Obsolete("This attribute is only to be used in C# language version 9.0 or later", true)] [AttributeUsage(AttributeTargets.Module, AllowMultiple = false)] public class ModuleInitializerAttribute : Attribute { public ModuleInitializerAttribute(Type type) { } } } ``` You would use it like this ``` c# [module: System.Runtime.CompilerServices.ModuleInitializerAttribute(typeof(MyModuleInitializer))] internal static class MyModuleInitializer { static MyModuleInitializer() { // put your module initializer here } } ``` and the C# compiler would then emit a module constructor that causes the static constructor of the identified type to be trigg
Comments (0)
No comments yet. Start the discussion.