惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

B
Blog RSS Feed
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
G
GRAHAM CLULEY
Hacker News - Newest:
Hacker News - Newest: "LLM"
C
Cybersecurity and Infrastructure Security Agency CISA
Simon Willison's Weblog
Simon Willison's Weblog
Latest news
Latest news
C
CERT Recently Published Vulnerability Notes
T
Threatpost
V
Vulnerabilities – Threatpost
AWS News Blog
AWS News Blog
Blog — PlanetScale
Blog — PlanetScale
C
Cisco Blogs
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
U
Unit 42
The Register - Security
The Register - Security
T
The Blog of Author Tim Ferriss
Stack Overflow Blog
Stack Overflow Blog
The Hacker News
The Hacker News
AI
AI
Project Zero
Project Zero
Scott Helme
Scott Helme
S
Securelist
Vercel News
Vercel News
GbyAI
GbyAI
S
Security @ Cisco Blogs
I
InfoQ
aimingoo的专栏
aimingoo的专栏
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
C
Check Point Blog
Forbes - Security
Forbes - Security
Google Online Security Blog
Google Online Security Blog
W
WeLiveSecurity
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
H
Heimdal Security Blog
G
Google Developers Blog
D
DataBreaches.Net
The Last Watchdog
The Last Watchdog
D
Docker
MyScale Blog
MyScale Blog
T
Tor Project blog
Cyberwarzone
Cyberwarzone
Recent Announcements
Recent Announcements
Microsoft Security Blog
Microsoft Security Blog
T
Tenable Blog
T
Threat Research - Cisco Blogs
Cisco Talos Blog
Cisco Talos Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
博客园 - 聂微东
月光博客
月光博客

博客园 - 寸芒

[原创]作业:家庭消费管理程序,一点代码 [原创]JScript学习摘要1 [随]程序设计模式的有趣解释-追MM [随]SQL Server 和ACCESS 、EXCEL 的数据转换 [原创]treeview简单根据数据库的数据,显示多级节点 [随]测试了下我的Blog价值(汗颜了) [随]C#轻松解决世纪迷题 [随]下了个windows live write 来玩玩 [随] 网站注册随机码的实现 [随]存储过程入门与提高 [原创]涂鸦事件 [随]c#.net常用函数和方法 [原创]到底调用哪个方法 [原创]浅要分析委托和事件 [原创]readonly 和const分析 [随]抽象基类与接口,共性与个性的选择 [随]c#连接数据库 [随]八荣八耻 [原创]引用类型的转换之感想
[原创]Box and Unbox
寸芒 · 2007-08-05 · via 博客园 - 寸芒

    利用ILDASM工具,清楚装箱和拆箱的执行的步骤,实例来自:

——————>是一本好书(推荐)

代码详细说明下,并总结!

先来看下面一段程序清单:

using System;

class cunmang

{

    static void Main(string[] args)

    {

        int foo=42;

        Object bar=foo;

    }

}

OK,编译一下,当然编译好的了的IL,下面用ILDASM工具打开刚才编译的,如图:

其中,有关IL的使用方法,请查看帮助,我这里只给个图片的截图:

对于本文章,其他的一些内容就不介绍了,我们直接看下主函数Main(),如图:

分析:首先说明这个是一个安全的代码,是托管的!

.locals init ([0] int32 foo, [1] object bar)

说明有2个变量,第一个是int32类型的 foo,一个是Object类型的 bar   IL_0000:  ldc.i4.s   42  说明把42装进堆栈里,以32位的形式存放。

      IL_0002:  stloc.0

后面的0说明是把第一个参数放进局部变量foo//(我们都知道,赋值运算是右结合性的,也就是说是从右往左运算的,所以这里的代码也就是我们写的foo=42;)

   IL_0003:  ldloc.0

把这个局部变量压到堆栈中去了,也就是说,在堆栈中用变量名foo来表示42

   IL_0004:  box        [mscorlib]System.Int32

看到box这个词了吗??也就是说开始装箱了,把原类型为(System.Int32)的foo进行装箱 。要知道原类型,我们可以使用foo.GetType()

IL_0009:  stloc.1

  IL_000a:  ret

参数1 就是把结果赋值给第二个参数,然后结束(ret)!

拆箱代码清单如下:

看一下多了什么?

  IL_0009:  stloc.1

  IL_000a:  ldloc.1

  IL_000b:  unbox      [mscorlib]System.Int32

  IL_0010:  ldind.i4

  IL_0011:  stloc.2

  IL_0012:  ret

// end of method cunmang::Main

OK,当执行到object bar=foo;的时候,执行  IL_000a:  ldloc.1 bar装进堆栈中去

  IL_000b:  unbox      [mscorlib]System.Int32

开始unbox(拆箱)32位的整型   IL_0011:  stloc.2 返回给第3个参数tbar

――――――――――――――――――――――――――――――――――――――――

总结:我听别人建议少用装箱和拆箱,在这里,当你改变foo的值,bar的值不变,或者改变bar的值,tbar的值却是不会改变,也就是说对于装箱还是拆箱,都是把原数据拷贝一份的,那么,装箱和拆箱用多了,也就存在浪费了!呵呵,这个是我的一点拙见,欢迎大家一起讨论!