我想知道为什么字符串文字都记录在程序集的清单中(在用户字符串标记下)?
看起来有点多余,考虑到字符串文字也直接在CIL中,即这个C#代码:
public void PrintMessage()
{
string myMessage = "Hello.";
Console.WriteLine(myMessage);
}
编译到此CIL
. method public hidebysig instance void PrintMessage() cil managed
{
.maxstack 1
// Define a local string variable (at index 0).
.locals init ([0] string myMessage)
// Load a string on to the stack with the value "Hello."
ldstr " Hello. "
// Store string value on the stack in the local variable.
stloc. 0
// Load the value at index 0.
ldloc. 0
// Call method with current value.
call void [mscorlib]System.Console: :WriteLine(string)
ret
}
该字符串文字也将被记录在程序集的清单中,类似于此内容(通过 ildasm 看到):
**User Strings**
-------------------------------------------------------
70000001 : (11) L"Hello."
那么为什么这两个地方都是字符串文字?它有什么用途?
P.S。这段代码完全由Pro C#2008和.NET平台
提供答案 0 :(得分:4)
我想你用来显示IL的工具会自动解析字符串并将其显示在原地吗?
答案 1 :(得分:4)
好吧,你看了两次 - 但是我期待 ldstr
指令只引用了字符串表中的字符串。它正在ildasm中显示,因为仅仅说“用户字符串7”等是相当繁琐的。