You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Mark methods of automatically implemented properties with CompilerGenerated attribute #11535
Is your feature request related to a problem? Please describe.
C# compilers mark methods of auto-implemented properties with CompilerGenerated attribute (example) and F# doesn't (example). Libraries can rely on the presence of this attribute to detect such properties. In particular, realm-dotnet rewrites methods of auto-implemented properties using Fody and ignores properties with explicit backing field.
I think, it's worth it to improve compatibility with C#-oriented libraries and allow to differentiate auto-implemented properties from explicit ones using reflection at cost of a few bytes of IL-code.
Describe the solution you'd like
Mark methods of auto-implemented properties.
Describe alternatives you've considered
On library side detection of auto-implemented properties can be extended by checking the presence of backing field with name <PropertyName>@. It's a hack but covers 99.9% of cases. Technically, detection can be broken by declaring field <PropertyName>@, but names with @ are reserved for compiler, so it's not really an issue.
Library consumers can use C# or implement explicit properties with backing field and mark property methods manually.
Additional context
.
Reacted by Ayrat Hudaygulov and Friedrich von Never
Is presence of the CompilerGenerated attribute on a property 100%/only means it is an auto-implemented property?
Looks like it, yes. There is a check that property is auto-implemented in Roslyn.
It seems to me the weavers would be better at looking at the IL for those scenarios to be robust, not just look at the attribute.
Well, auto-implemented property is just a 'syntax sugar'. It's IL-implementation does not differ that much from explicit.
As you said, backing fields can be named differently, the same thing applies to property accessors (at least, this comment implies it) and method bodies are simple. In In this circumstance I see attribute as a universal contract that library authors can rely on instead of adjusting to every language.
As a side note, methods that compiler generates for records (Equals(), ToString() and so on) are marked as CompilerGenerated, but properties and backing fields are not. Not sure if it's intentional design or just shared implementation.
Looks like it, yes. There is a check that property is auto-implemented in Roslyn.
If I read that roslyn code correctly, it is the compiler writing the IL adding the attribute on the property, I don't take this as an immutable and unambiguous statement that the attribute "is only ever going to be used on properties when those are auto implemented", when this somehow changes, this is going to break some weavers 🙂.
In In this circumstance I see attribute as a universal contract that library authors can rely on instead of adjusting to every language.
I'd say a thing which holds true for 100% of current C# codebases, but not sure what the weavers are used for, hard for me to trust the attribute on a property will always mean what it is used for in that context.
Nonetheless, by all means, if you want to contribute to the compiler to emit it, it seems the AutoProperty carries into the AST, and figuring out the code gen part for them shouldn't be too hard.
I hope you can get confirmation from the team that such PR with few tests and required changes would be merged, but you may hack the implementation yourself to see what it takes and open a PR.
Activity
smoothdeveloper commentedon May 6, 2021
Here is an example of a weaver (not related with realm AFAIU):
https://github.com/robinzevallos/kasay-propertychanged/blob/1b86901da71b6767c21bc164072bc6b0b0d5d1e3/Kasay.PropertyChanged.Fody/ModuleWeaver/PropertyChangedWeaver.cs
this one seem to rely on the C# backing field name, which wouldn't work with VB.NET (https://sharplab.io/#v2:DYLgbgRgNAJiDUAfAwsAhgZwwAgCoE8AHAUwFgAobK7ABQCcB7EugF32wDEBLOjFgOTQBbYtgCCOAMos6XAHYBzCgFE5MbKkwYgA).
So it seems the weavers are all going with their own approach at identifying things they want to weave.
It makes sense to add the attribute to align with the two other languages.
Is presence of the
CompilerGeneratedattribute on a property 100%/only means it is an auto-implemented property?It seems to me the weavers would be better at looking at the IL for those scenarios to be robust, not just look at the attribute.
BoundedChenn31 commentedon May 7, 2021
Looks like it, yes. There is a check that property is auto-implemented in Roslyn.
Well, auto-implemented property is just a 'syntax sugar'. It's IL-implementation does not differ that much from explicit.
As you said, backing fields can be named differently, the same thing applies to property accessors (at least, this comment implies it) and method bodies are simple. In In this circumstance I see attribute as a universal contract that library authors can rely on instead of adjusting to every language.
As a side note, methods that compiler generates for records (Equals(), ToString() and so on) are marked as
CompilerGenerated, but properties and backing fields are not. Not sure if it's intentional design or just shared implementation.smoothdeveloper commentedon May 7, 2021
If I read that roslyn code correctly, it is the compiler writing the IL adding the attribute on the property, I don't take this as an immutable and unambiguous statement that the attribute "is only ever going to be used on properties when those are auto implemented", when this somehow changes, this is going to break some weavers 🙂.
I'd say a thing which holds true for 100% of current C# codebases, but not sure what the weavers are used for, hard for me to trust the attribute on a property will always mean what it is used for in that context.
Nonetheless, by all means, if you want to contribute to the compiler to emit it, it seems the
AutoPropertycarries into the AST, and figuring out the code gen part for them shouldn't be too hard.I hope you can get confirmation from the team that such PR with few tests and required changes would be merged, but you may hack the implementation yourself to see what it takes and open a PR.
dsyme commentedon Apr 20, 2022
We should do this, it's a good first issue for people actually
Emitting CompilerGenerated attribute on auto-generated accessors dotn…
Mark methods of automatically implemented properties with CompilerGe…