Skip to content

Calling protected static base member from static do raises MethodAccessException #11929

@marklam

Description

@marklam

If the static do block of a class attempts to call a protected static member of a base class, a MethodAccessException is raised because the code from the do block is moved outside the derived class by the compiler

#nowarn "44" // using Uri.EscapeString just because it's protected static

type C(str : string) =
    inherit System.Uri(str)
    
    static do
        System.Uri.EscapeString("data") |> ignore

C("hello") |> ignore

Sharplab example

Expected behavior

The method call should succeed, since the static do is 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 MethodAccessException is raised on the call

Known workarounds
Move the method call into a static member of the derived class, and call it from the static do block:

    static do
        C.Do()

    static member internal Do() =
        System.Uri.EscapeString("data") |> ignore

Metadata

Metadata

Assignees

No one assigned

    Labels

    Area-Compiler-CheckingType checking, attributes and all aspects of logic checkingBugImpact-Medium(Internal MS Team use only) Describes an issue with moderate impact on existing code.

    Type

    Projects

    Status

    New

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions