Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeLoadException when struct contains nullable static field of itself #79636

Closed
gurksaft opened this issue Dec 14, 2022 · 1 comment
Closed

Comments

@gurksaft
Copy link

gurksaft commented Dec 14, 2022

Description

If a struct contains a static field that is of the same type as itself and also nullable then referencing the type during runtime will result in a TypeLoadException with no explanation to why it fails to load the type.

Reproduction Steps

Program.cs

System.Console.WriteLine(typeof(TestStruct1));

TestStruct1.cs

public struct TestStruct1
{
	public static TestStruct1? Instance;
}

ConsoleApp1.csproj

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net7.0</TargetFramework>
  </PropertyGroup>
</Project>

Expected behavior

Remarking the field TestStruct1.Instance results in the expected behavior which is that the name of the struct "TestStruct1" is rendered to console. Removing the Nullable-part or changing to another struct (as Nullable) of the field type works as intended.

Actual behavior

TypeLoadException when the type is referenced in runtime. Changing from typeof(TestStruct1) to new TestStruct1() in Program.cs makes no difference.

Regression?

No response

Known Workarounds

Write code that does not use this pattern at all; struct with static nullable field of itself.

Configuration

Tested with:

  • .NET 6 and 7
  • Windows 10 and 11
  • Visual Studio 2022 17.4.3 and 14.7.0
  • Intel x64 CPU
  • Published Release build and debug

Other information

No response

@ghost ghost added the untriaged New issue has not been triaged by the area owner label Dec 14, 2022
@DaZombieKiller
Copy link
Contributor

This is a duplicate of #6924.

@teo-tsirpanis teo-tsirpanis closed this as not planned Won't fix, can't repro, duplicate, stale Feb 27, 2023
@ghost ghost removed the untriaged New issue has not been triaged by the area owner label Feb 27, 2023
@ghost ghost locked as resolved and limited conversation to collaborators Mar 29, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants