什么是引擎盖下投引擎盖

2023-09-03 16:07:42 作者:不忘旧约つ

可能重复:    C#作为剧组VS经典投

我想知道.NET CLR的引擎盖下会发生什么,当我做这样的事情

I want to know what happens under the hood of the .Net CLR when I do something like


object myObj = "abc";
string myStr = (string)myObj;

和如何在第二行不同于字符串myStr的= myObj.ToString()字符串myStr的= MyObj中的字符串;

and how the second line differs from string myStr = myObj.ToString() or string myStr = myObj as string;

环顾四周,我发现仿制药的答案,如编译器插入code有,但我不太满意......我要找的演员力学深undertanding ...哦,编译器插入code?给我看看!编译器optmizes的code?怎么样?当?

looking around I found generics answers such as "the compiler inserts code there" but I'm not satisfied... I'm looking for deep undertanding of the cast mechanics... Oh the compiler inserts code? Show me! The compiler optmizes the code? How? When?

PLS获得尽可能接近金属,你可以!!!

Pls get as close to the metal as you can!!!

推荐答案

您可以使用的 IL Dissasembler 的,看看有什么正在制作由您code在一个较低的水平。如果您有Visual Studio安装在您的计算机上,你应该能够找到它只是在Windows搜索框中输入ILDASM。

You can use IL Dissasembler to see what is being produced by your code on a lower level. If you have Visual Studio installed on your machine, you should be able to find it just by typing "ildasm" in the windows search box.

下面就是以下code中的IL是这样的:

Here's what the IL of the following code looks like:

object myObj = "abc";
string myStr = (string)myObj;    
string tostring = myObj.ToString();