gpt4 book ai didi

c# - "FatalExecutionEngineError was detected"在重新定义 String.Empty 后使用 Console.Writeline

转载 作者:太空宇宙 更新时间:2023-11-03 11:36:29 25 4
gpt4 key购买 nike

我正在尝试(为了好玩)将 String.Empty 重新定义为单个空格“”。为什么这会破坏 CLR 框架?

留言:

The runtime has encountered a fatal error. The address of the error was at 0x5814b976, on thread 0xf40. The error code is 0x80131623. This error may be a bug in the CLR or in the unsafe or non-verifiable portions of user code. Common sources of this bug include user marshaling errors for COM-interop or PInvoke, which may corrupt the stack.

如何重现:

class Program
{
static void Main()
{
typeof(string).GetField("Empty").SetValue(null, " ");
Console.WriteLine("{}", "");
}
}

最佳答案

如果我们看程序

class Program
{
static void Main()
{
Console.WriteLine("{}", "");
}
}

然后它将失败并出现 FormatException 错误消息 Input string was not in a correct format

然而,当我们在 Console.WriteLine 行之前插入 typeof(string).GetField("Empty").SetValue(null, ""); 时代码在尝试查找该错误消息时失败。如果我们查看完整的堆栈跟踪(包括“显示外部代码”),那么我们会看到代码在 System.Resources.ManifestBasedResourceGroveler.HandleResourceStreamMissing(string fileName = "mscorlib.resources") 处失败(注意mscorlib.resources 前面的空格)。

这是因为ManifestBasedResourceGroveler使用了ResourceManager的方法GetResourceFileName来查找资源文件。在 GetResourceFileName 中,我们使用 StringBuilder 构造文件名,StringBuilder 的构造函数以 String.Empty 开头。

public StringBuilder(int capacity)
: this(String.Empty,capacity){
}

关于c# - "FatalExecutionEngineError was detected"在重新定义 String.Empty 后使用 Console.Writeline,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6106989/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com