Open huoyaoyuan opened 1 year ago
It is valid to pass a struct to GC.KeepAlive
if you want to keep its object fields alive, and I heard that the JIT optimizes it to not box.
and I heard that the JIT optimizes it to not box.
Correct -- #54412.
Tagging subscribers to this area: @tommcdon See info in area-owners.md if you want to be subscribed.
Author: | huoyaoyuan |
---|---|
Assignees: | - |
Labels: | `api-suggestion`, `area-System.Diagnostics`, `untriaged`, `needs-area-label` |
Milestone: | - |
Background and motivation
There are APIs that accepts
object
and operates with object identity, thus boxed structs will not work.Examples including:
GC.KeepAlive (For keeping the collectible type alive, willKeepAlive(value.GetType())
be better?)ArgumentNullException.ThrowIfNull (#85154) is similar, but the motivation is different, and will have different meaning with nullable value type.
With such an attribute, we can have one analyzer for all these APIs, without recognizing them one by one, and also works for user code. Can it be reviewed together?
API Proposal
The analyzer will warn if the declared type of the parameter is value type.
API Usage
Alternative Designs
Should it be named as what's expected, or what's unexpected?
Should it be inherited?
Should we include a custom diagnostic id for analyzer? Users are unlikely to suppress such warnings though.
Risks
No response