9

Visual Studio 2015的坑:中文字符串编译后成乱码

 3 years ago
source link: https://www.cnblogs.com/cmt/p/4692920.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Visual Studio 2015的坑:中文字符串编译后成乱码

(2015年8月5日更新:微软已经修复了Roslyn的这个bug,详见 https://github.com/dotnet/roslyn/pull/4303

昨天,我们用VS2015编译了博客程序中的一个程序集并发布上线。

今天有园友反馈向我们反馈,个人博客分页显示随笔列表的页面中,“上一页”“下一页”显示乱码:

而这个地方的“上一页”“下一页”字符串恰恰是在我们昨天发布的程序集中定义的:

public class Pager : Control
{
    protected string PreviousText = "上一页";
    protected string NextText = "下一页";

    //...
}

可是昨天我们并没有更改这部分代码,肯定不是我们昨天代码修改引起的。

于是,我们改用VS2013重新编译了一下这个程序集,更新之后,乱码立马消失。

接着,用ILSpy反编译了VS2015所编译出的程序集的IL代码之后,真相大白:

public class Pager : Control
{
    protected string PreviousText = "ÉÏÒ»Ò³";
    protected string NextText = "ÏÂÒ»Ò³";
    //...
}

原来是VS2015所用的编译器惹的祸,而这个编译器就是大名鼎鼎的 Roslyn

大家使用 Visual Studio 2015 时需要注意一下这个问题。

用ildasm查看VS2015编译出来的程序集的IL代码(乱码):

.maxstack  2
IL_0000:  ldarg.0
IL_0001:  ldstr      bytearray (C9 00 CF 00 D2 00 BB 00 D2 00 B3 00 ) 
IL_0006:  stfld      string BlogServer.Web.Controls.Pager::PreviousText
IL_000b:  ldarg.0
IL_000c:  ldstr      bytearray (CF 00 C2 00 D2 00 BB 00 D2 00 B3 00 ) 
IL_0011:  stfld      string BlogServer.Web.Controls.Pager::NextText

用ildasm查看VS2013编译出来的程序集的IL代码(未乱码):

.maxstack  2
IL_0000:  ldarg.0
IL_0001:  ldstr      bytearray (0A 4E 00 4E 75 98 )                               // .N.Nu.
IL_0006:  stfld      string BlogServer.Web.Controls.Pager::PreviousText
IL_000b:  ldarg.0
IL_000c:  ldstr      bytearray (0B 4E 00 4E 75 98 )                               // .N.Nu.
IL_0011:  stfld      string BlogServer.Web.Controls.Pager::NextText

【问题原因与临时解决方法】

在GitHub上提交Issue之后,从回复中得知这个问题与Roslyn检测文件编码的处理方式有关。

查看出现乱码问题的.cs文件编码,发现用的是ANSI编码。于是以UTF-8编码另存该文件,然后用VS2015重新编译,问题解决。

VS2015 RC中没这个问题。

【GitHub上的相关链接】

* VS2015打开非unicode编码的代码,其中变量名有中文就无法编译的bug

* .NET compiler produces incorrect string constants in MSIL when C# source files encoded with non-UTF-8 encoding

* VS2015 (MSBuild/14) compiler can't detect file ecoding correctly

* Chinese string is compiled to garbage characters

* Roslyn can't detect 932 encoding


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK