-
Notifications
You must be signed in to change notification settings - Fork 1k
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
[Proposal]: Attributes on Main for top level programs #5045
Comments
cc @jaredpar |
Another alternative would be to simply use the |
What do you mean by "same location" in this context? |
Another alternative that I personally like is |
Same file, likely above the statements. |
Putting it directly above the statements would syntactically attach the attributes to the first top-level statement, which seems like a good thing to me. At that point it’s not clear if it’s necessary to include a ‘method’/‘main’/’entrypoint’ target, although it may be preferred for clarity. Plus we may want to reserve the meaning of an attribute on a statement for some purpose in the future. |
I would have these parse like assembly level attributes. Those don't attach to the namespace member decl, they go into their own section of the tree. In this case I would not have these attach to a statement |
It is important that this attribute can be generated using source generator and thus live in a separate file. If it is not possible to generate this attribute using source generator, this feature loses a lot of its appeal for streamlined WinForms top level programs. |
This comment was marked as outdated.
This comment was marked as outdated.
There are several open questions on this feature. For the purposes of the prototype we're going with the following designs, but will revisit them in a future LDM to determine their final answers. Syntax: Should it be
Scope: Should you be able to use
Targets: Should we introduce a corresponding
|
Interesting approach on those designs. Does this mean that, completely hypothetically, the WinForms SDK could define |
This is going to be very tricky to implement. We determine the entry-point very late and now we will need to figure this out just to be able to bind attributes. I am not sure if the complexity worth it. |
@AlekseyTs Couldn't attributes with the |
Our current model is to be able to bind attributes related to a specific symbol on demand, pretty much any time. Today, all such attributes are syntactically related to symbol's declaration. All attributes are supposed to be returned through public API ( |
Is this part really required? The STA/MTA choice affects how threading works in COM interop and troubleshooting related issues, so it's better to have STAThread attribute visible next to the code it applies to. In case of WinForms it's always there and it does not matter, but for console apps i'd like to always determine quickly whether it enables STA. Also, would WinForms benefit much from auto-generating STAThread attribute only? I think the whole Program.cs in WinForms apps is a boilerplate that people usually don't modify manually. Could WinForms work like WPF, where user sets startup window declaratively, and entry point is synthesized automatically with correct attributes? |
I don't think most WinForms developers, or developers in general, use COM interop, or even know what it is. For the majority of devs, that attribute is just some weird funky thing that you're not supposed to touch. |
But I agree that for WinForms, the whole entry point is something that could be generated and that people usually don't touch and aren't interested in. |
Visual Basic hides the entry point with most configurations done via the Application Framework. That makes most (if not all) VB developers unaware of how the application is configured, and that creates frictions. So hiding the Program.cs or ther
I think you'd be surprised. |
Kicking up the dust about this. The comments from the LDM 2022-03-09 meeting focused on the Found this thread and sharing this as I am writing a program now which is reporting Guess I'll revert to the traditional |
@Sour-Codes The recommended way to enable preview features is to do it for the whole assembly by adding |
You can also do: [RequiresPreviewFeatures]
partial class Program { } |
@svick, this approach works if you're expecting to use preview features throughout one project. My scenario is that my library also utilizes
|
@333fred Works like a charm! 🎉🥳I missed the memo that top-level statements creates a
|
Can’t speak for others, but when I was doing VB WInForms I was aware of and using both…and have no reason to expect that the application’s aren’t still in use, so whoever is maintaining them now probably is as well. |
Attributes on Main for top level programs
Summary
Allow attaching attributes to Main method in top level programs.
Motivation
The primary motivation for this feature is tenabling top level programs for WinForms (see https://github.com/dotnet/designs/blob/main/accepted/2021/winforms/streamline-application-bootstrap.md). WinForms programs require the Main method to be annotated with
[STAThread]
.Detailed design
Add
main
as the new global attribute target (assembly
andmodule
are the existing global attribute targets). Attribute specified with this target will be attached to the program Main method.Example:
[main: STAThread]
The main attribute target can be used in any compilation unit. In particular, it does need to be in the same compilation file as the top level program. This is required to enable the streamlined WinForms top level programs - the attribute will be generated by source generator.
It is an error to use the
main
global attribute target in libraries.Drawbacks
Alternatives
The alternative is to pass the information that is encoded via attributes on Main method via alternative channel in top level programs (e.g. attributes on assembly or config file) and modify runtime, libraries and tools to look for this information in the alternative location in addition to the existing location.
Unresolved questions
Can this be used to attach attributes to Main method even in non-top-level programs?
Design meetings
The text was updated successfully, but these errors were encountered: