Skip to content
This repository was archived by the owner on Sep 25, 2026. It is now read-only.

Add test which accessed property backing field via reflection - #2629

Merged
vitek-karas merged 1 commit into
dotnet:feature/damAnalyzerfrom
vitek-karas:BackingFieldTest
Feb 18, 2022
Merged

vitek-karas merged 1 commit into
dotnet:feature/damAnalyzerfrom
vitek-karas:BackingFieldTest

Conversation

@vitek-karas

@vitek-karas vitek-karas commented Feb 17, 2022 •

Copy link
Copy Markdown
Member

@jtschuster jtschuster left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious, why use derivedInstance.GetType () instead of typeof (DerivedWithAnnotation) in TypeHierarchyReflectionWarnings.cs?

@tlakollo

Copy link
Copy Markdown
Contributor

Just curious, why use derivedInstance.GetType () instead of typeof (DerivedWithAnnotation) in TypeHierarchyReflectionWarnings.cs?

typeof() is an operator to obtain a type known at compile-time, the behavior is consistent from the standpoint of static analysis so the linker relies on the exact knowledge of the type. And we ignore the fact that the type can be annotated for this case.
Object.GetType() gets the exact runtime type of the current instance. The behavior can vary and therefore we have different ways to handle it. For example, if the type is sealed, Object.GetType() will behave like typeof() ignoring the annotations on the type if any. The difference is that in certain scenarios we would have to fulfill the annotation on the type.
It's not a simple scenario, but at least is somewhat documented in the code and a little bit less covered in this doc

@vitek-karas

Copy link
Copy Markdown
Member Author

Thanks @tlakollo for the explanation - it's spot on.

@vitek-karas
vitek-karas merged commit 2116ace into dotnet:feature/damAnalyzer Feb 18, 2022
@vitek-karas
vitek-karas deleted the BackingFieldTest branch February 18, 2022 10:57
agocke pushed a commit to dotnet/runtime that referenced this pull request Nov 16, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants