-
Notifications
You must be signed in to change notification settings - Fork 880
Calling protected static base member from static do raises MethodAccessException #11929
Copy link
Copy link
Open
Open
Copy link
Labels
Area-Compiler-CheckingType checking, attributes and all aspects of logic checkingType checking, attributes and all aspects of logic checkingArea-Compiler-Checking-AccessibilityVisibility checks: internal, private, protected, InternalsVisibleToVisibility checks: internal, private, protected, InternalsVisibleToArea-Compiler-CodeGenIlxGen, ilwrite and things at the backendIlxGen, ilwrite and things at the backendBugImpact-Medium(Internal MS Team use only) Describes an issue with moderate impact on existing code.(Internal MS Team use only) Describes an issue with moderate impact on existing code.
Milestone
Description
Activity
Metadata
Metadata
Assignees
Labels
Area-Compiler-CheckingType checking, attributes and all aspects of logic checkingType checking, attributes and all aspects of logic checkingArea-Compiler-Checking-AccessibilityVisibility checks: internal, private, protected, InternalsVisibleToVisibility checks: internal, private, protected, InternalsVisibleToArea-Compiler-CodeGenIlxGen, ilwrite and things at the backendIlxGen, ilwrite and things at the backendBugImpact-Medium(Internal MS Team use only) Describes an issue with moderate impact on existing code.(Internal MS Team use only) Describes an issue with moderate impact on existing code.
Type
Projects
- StatusShow more project fieldsNew
If the
static doblock of a class attempts to call aprotected staticmember of a base class, a MethodAccessException is raised because the code from the do block is moved outside the derived class by the compilerSharplab example
Expected behavior
The method call should succeed, since the
static dois the equivalent to C#'s static constructor, or a warning should be produced that while this will compile it won't work.Actual behavior
A
MethodAccessExceptionis raised on the callKnown workarounds
Move the method call into a static member of the derived class, and call it from the
static doblock: