prevent改变的String.Empty值 [英] Prevent changing the value of String.Empty

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

问题描述

部分从一个奇怪的断事的角度来看,部分来源于对潜在问题的一个保障。想象一下,是可以通过调用以下(或类似的东西,但的String.Empty 是一个很好的例子)发生的最坏的:

Partially from a curious breaking things point of view and partially from a safeguarding against potential problems. Imagine what is the worst that can happen by calling the following (or something similar, but string.Empty is a good examples):

typeof(String).GetField("Empty", 
    BindingFlags.Public | 
    BindingFlags.NonPublic | 
    BindingFlags.Static | 
    BindingFlags.GetField
).SetValue(null, "foo" );

这会造成问题时,有code的地方,确实 X = myClass.bar?的String.Empty

This would cause problems when there's code somewhere that does x = myClass.bar ?? string.Empty.

有什么办法(类似于不同的应用程序域或类似),以防止(或检测)人变更如的String.Empty 的值或者在 SqlDateTime.MinValue (或其他类似的.NET只读域)?

Is there any way (akin to different app domains or similar) to protect against (or detect) someone changing values like String.Empty or perhaps the SqlDateTime.MinValue (or other similar readonly fields in .NET)?

推荐答案

您可以定义自己的常量在领域的的组件,如下所示:

You could define your own const fields in your assembly like so:

internal const string Empty = "";

,然后执行对<一检查href="http://msdn.microsoft.com/en-us/library/system.string.empty.aspx"><$c$c>String.Empty,但它是pretty的多徒劳无功。

And then perform a check against String.Empty, but it's pretty much a futile effort.

您不得不到处访问执行检查的String.Empty ,有效地取代它(因为你在查询比较的角度来看,有中没有点实际使用的String.Empty 在您的版本了)。

You'd have to perform a check everywhere you access String.Empty and effectively replace it (since you'd check at the point of comparison, there's no point in actually using String.Empty over your version anymore).

由于它是常量,价值是不能变更的,这是很好的,但在同一时间内,如果字符串的值。空确实的不断变化(但不太可能),你就必须做出改变你的值,然后重新编译的所有的引用该场

Since it's const, the value couldn't be changed, which is good, but at the same time, if the value of String.Empty does ever change (however unlikely), you'll have to make the change to your value, and then recompile everything that references that field.

,你可以让你的版本只读,但你会很容易受到具有价值的反射发生变化,你是正确的回到开始的地方

Of course, you could make your version readonly, but then you'd be vulnerable to having the value changed through reflection, and you're right back where you started.

添加到这一事实,如果的String.Empty 被改变的话,不仅对现场的比较会受到影响,但我想象的巨大在BCL只想不能正常工作的方法号;翻翻反射,似乎它引用在mscorlib中(.NET 4.0)几百倍单独的 的:

Add to that the fact that if String.Empty was changed, not only would comparisons against the field be affected, but I'd imagine a tremendous number of methods in the BCL would just not work properly; looking through Reflector, it would seem it's referenced a few hundred times in mscorlib (.NET 4.0) alone:

这样说,你的可以尝试的,并确保该值没有改变,但它只是是不实际的(这将是猫捉老鼠的永恒游戏),机会是,从程序的角度看,如果的String.Empty 的值被改变,世界将结束,并计划将几乎就死一个可怕的死亡很快因为它已被更改。

So that said, you could try and guarantee that the value wasn't changed, but it just isn't practical (it will be an eternal game of cat-and-mouse), chances are, from the perspective of your program, if the value of String.Empty was changed, the world would end, and the program would die a horrible death fairly quickly almost as soon as it was changed.

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

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