为什么不能更改String.Empty的值? [英] Why can't I change the value of String.Empty?

查看:32
本文介绍了为什么不能更改String.Empty的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

虽然我知道更改 String.Empty 的值将是一个坏主意,但我不明白为什么我不能这样做.

While I understand that changing the value of String.Empty would be a bad idea, I don't understand why I can't do it.

要理解我的意思,请考虑以下课程:

To get what I mean, consider the following class:

public class SomeContext 
{ 
    static SomeContext(){}
    public static readonly string Green = "Green";
    public static readonly SomeContext Instance = new SomeContext();

    private SomeContext(){}
    public readonly string Blue = "Blue";

    public static void PrintValues()
    { 
        Console.WriteLine(new { Green, Instance.Blue, String.Empty }.ToString());
    }
}

我有一个小的控制台应用程序,试图操纵这三个只读字段.它可以成功地将蓝色和绿色变成粉红色,但空"保持不变:

I have a little console app that tries to manipulate these three readonly fields. It can successfully make Blue and Green into Pink, but Empty stays the same:

        SomeContext.PrintValues();
        ///  prints out : { Green = Green, Blue = Blue, Empty = }
        typeof(SomeContext).GetField("Blue").SetValue(SomeContext.Instance, "Pink");
        typeof(SomeContext).GetField("Green", BindingFlags.Public | BindingFlags.Static).SetValue(null, "Pink");
        typeof(String).GetField("Empty", BindingFlags.Public | BindingFlags.Static).SetValue(null, "Pink");
        SomeContext.PrintValues();
        ///  prints out : { Green = Pink, Blue = Pink, Empty = }

为什么?

最初,我还问为什么 String.Empty 不是一个常量.我在另一篇文章中找到了问题这部分的答案,并删除了该部分问题的一部分).

Why?

Originally, I also asked why String.Empty wasn't a constant. I found the answer to this part of my question on another post and deleted that part of the question).

注意:重复项"都没有针对此问题的最终答案,这就是为什么我要问这个问题.

推荐答案

您无法更改它,因为您的计算机上装有.NET 4.5.只需将项目的Framework Target设置更改为3.5,即可看到它的工作原理.

You can't change it because you have .NET 4.5 on your machine. Just change the Framework Target setting of your project to 3.5 and you'll see it works.

CLR具有String.Empty的内置知识.例如,您将使用Reflector或Reference Source看到System.String类从不对其进行初始化.它是在CLR启动期间完成的.4.5到底如何防止修改可见是很难说的.您实际上是 did 修改了该字段,只需添加以下代码行即可:

The CLR has built-in knowledge of String.Empty. You'll for example see with a Reflector or the Reference Source that the System.String class never initializes it. It is done during CLR startup. Exactly how 4.5 prevents the modification from being visible is a bit hard to tell. You actually did modify the field, just add this line of code:

  var s = typeof(String).GetField("Empty", 
             BindingFlags.Public | BindingFlags.Static).GetValue(null);
  Console.WriteLine(s);

您将看到粉红色".我的猜测是它被抖动拦截了.但是,我无法给出确凿的证据.有先例,例如尝试修改Decimal.MaxValue,这是另一个只读静态值.抖动也无法在3.5中更改,抖动会识别它并直接生成值,而无需读取该字段.

And you'll see "Pink". My guess is that it is intercepted by the jitter. I can't give hard evidence for that however. There's precedent, try tinkering with Decimal.MaxValue for example, another readonly static value. Not changeable in 3.5 either, the jitter recognizes it and generates the value directly without reading the field.

我刚刚发现您对

I just found out you put a bounty on another question with the exact same subject. 280Z28's post is pretty similar.

这篇关于为什么不能更改String.Empty的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆