我有这个Unicode字符串: ??????????????????
而且我想通过字符分开.现在,如果我尝试循环所有字符的真相,我得到这样的东西:
A a a ' ? ...
有没有办法将此字符串正确分割为字符: ? ? ???
我经常想这样做:
public void Foo(Bar arg)
{
throw new ArgumentException("Argument is incompatible with " + name(Foo));
}
Run Code Online (Sandbox Code Playgroud)
因为如果我更改了Foo的名称,IDE也会重构我的错误消息,如果我将方法的名称(或任何其他类型的成员标识符)放在字符串文字中,将不会发生什么.我知道实现"名称"的唯一方法是使用反射,但我认为性能损失超过了可持续性增益,并且它不会涵盖所有类型的标识符.
括号之间的表达式的值可以在编译时计算(如typeof),并通过更改语言规范进行优化以成为一个字符串文字.你认为这是一个有价值的功能吗?
PS:第一个例子看起来问题只与例外有关,但事实并非如此.想想您可能想要引用类型成员标识符的每种情况.你必须通过字符串文字来做,对吗?
另一个例子:
[RuntimeAcessibleDocumentation(Description="The class " + name(Baz) +
" does its job. See method " + name(DoItsJob) + " for more info.")]
public class Baz
{
[RuntimeAcessibleDocumentation(Description="This method will just pretend " +
"doing its job if the argument " + name(DoItsJob.Arguments.justPretend) +
" is true.")]
public void DoItsJob(bool justPretend)
{
if (justPretend)
Logger.log(name(justPretend) + "was true. …Run Code Online (Sandbox Code Playgroud)