开发者

Why is every string literal documented in an assembly's manifest?

I wonder why string literals are all documented in an assembly's manifest (under the User Strings token)?

It seems a bit redundant, considering that the string literal is also in the CIL directly, i.e. this C# code:

public void PrintMessage()
{
    string myMessage = "Hello.";
    Console.WriteLine(myMessage);
}

compiles to this 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
}

That string literal also will be recorded in the assembly's manifest wit开发者_JAVA百科h something like this (as seen through ildasm):

**User Strings**
-------------------------------------------------------
70000001 : (11) L"Hello."

So why is the string literal in both places? What purpose does it serve?

P.S. this code is all courtesy of Pro C# 2008 and the .NET Platform


Well, you're seeing it twice - but I would expect that the ldstr instruction just referenced a string from the string tables. It's being shown in the ildasm because it would be pretty tedious to just say "user string 7" etc.


I suppose the tool you're using to display the IL automatically resolves the strings and displays it inplace?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜