对您来说它看起来像 C# 错误吗?
Does it look like a C# bug for you?
创建一个控制台应用来重现:
struct Test
{
public static readonly Test? Null = null;
}
class Program
{
static void Main(string[] args)
{
var t = Test.Null;
}
}
它是可编译的,但我们将在 运行 时得到以下内容:
An unhandled exception of type 'System.TypeLoadException' occurred in mscorlib.dll. Additional information: Could not load type 'ConsoleApplication17.Test' from assembly 'ConsoleApplication17, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.
这种方法解决了问题:
struct Test
{
public static Test? Null => null;
}
This is known implementation limitation in CoreCLR - both the instance and static field layout is done together that results into this error. It is not easy to fix.
来源:Static fields should not contribute to cyclic struct layout #4049
创建一个控制台应用来重现:
struct Test
{
public static readonly Test? Null = null;
}
class Program
{
static void Main(string[] args)
{
var t = Test.Null;
}
}
它是可编译的,但我们将在 运行 时得到以下内容:
An unhandled exception of type 'System.TypeLoadException' occurred in mscorlib.dll. Additional information: Could not load type 'ConsoleApplication17.Test' from assembly 'ConsoleApplication17, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.
这种方法解决了问题:
struct Test
{
public static Test? Null => null;
}
This is known implementation limitation in CoreCLR - both the instance and static field layout is done together that results into this error. It is not easy to fix.
来源:Static fields should not contribute to cyclic struct layout #4049