-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Closed
Description
Description
A System.TypeLoadException exception is thrown when an interface contains either a static field or property with an initial value for a derived struct type. This works for derived record and class types.
Reproduction Steps
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>
</Project>public interface IExample
{
public static Example DefaultExample { get; } = new();
}public struct Example : IExample { }var example = IExample.DefaultExample;Unhandled exception. System.TypeLoadException: Could not load type 'Example' from assembly 'test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.
at Program.<Main>$(String[] args)
Expected behavior
Behavior should be consistent for where an interface can be statically aware of derived types.
Actual behavior
Behavior is inconsistent for where an interface can be statically aware of derived types.
Regression?
No response
Known Workarounds
See "Other Information" section.
Configuration
No response
Other information
If the struct type is changed to a record or class type then the error is not seen.
E.g.
public record Example : IExample { }Or
public class Example : IExample { }If the static property or field is changed from having an initial value to instead be a property with a getter or an expression-bodied property then the error is not seen.
E.g.
public static Example DefaultExample { get => new(); }Or
public static Example DefaultExample => new();neon-sunset, Takym, walterlv, JakeYallop, BrunoJuchli and 1 more