Skip to content

MissingMethodException when modify record in solution with mixed TFMs #79082

@MaceWindu

Description

@MaceWindu

Description

It is possible to get MissingMethodException exception for init-only properties in some build configurations (see below)

Ready-to-run code:
ConsoleApp48.zip

Reproduction Steps

To use records in code, built against pre-net5.0 TFMs we need to define IsExternalInit class manually in those projects.

To reproduce issue we need at least two TFMs:

  • one that doesn't have IsExternalInit, we will use netstandard2.0 as example
  • one with IsExternalInit, let's use net7.0

Solution configuration:

  • project A with both TFMs, record and IsExternalInit wrapped in #if !NET5_0_OR_GREATER (that's important part!)
  • project B with netstandard2.0 TFM, record modification code using r with { Prop = new value} and refence to project A (for record definition)
  • project C with net7.0 TFM calling modification code from project B with references to both project A and project B (another importand part!)

When we build project C we also get:

  • netstandard2.0 binary from project B
  • net7.0 binary from project A (if we don't have direct reference to A, it will be nestandard2.0 transitive build)

Trying to run project C binary will result in MissingMethodException exception which looks to me like some unnecessary (or not?) strict modreqs verification

Expected behavior

It just works (C) Todd Howard

Actual behavior

Unhandled exception. System.MissingMethodException: Method not found: 'Void TestRecord.set_Test(System.String)'.
   at Test.TestRecord()
   at Program.Main()

Regression?

No response

Known Workarounds

Workaround is actually easy - don't use conditional compilation when adding IsExternalInit to project so all TFM artifacts will use same class. Fortunatelly because this is internal class, it is possible to do without conflicts if you don't use InternalsVisibleTo

Configuration

No response

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions