Emit學習-答疑篇-實值型別和參考型別在使用時的區別

來源:互聯網
上載者:User

mso-ascii-font-family:"Times New Roman";mso-hansi-font-family:"Times New Roman"'>今天下午興沖沖的寫完一段ILmso-hansi-font-family:"Times New Roman"'>代碼,用Reflector"Times New Roman"'>轉成C#宋體;mso-ascii-font-family:"Times New Roman";mso-hansi-font-family:"Times New Roman"'>代碼看了下,沒有問題,於是引用持久化到硬碟上的動態程式集,想要試一下其中的方法,但是運行後卻出現了System.AccessViolationExceptionmso-ascii-font-family:"Times New Roman";mso-hansi-font-family:"Times New Roman"'>,提示資訊為:“mso-hansi-font-family:"Times New Roman"'>嘗試讀取或寫入受保護的記憶體。這通常指示其他記憶體已損壞。”mso-hansi-font-family:"Times New Roman"'>。看著錯誤提示一陣頭大,新配的電腦記憶體損壞是不太可能了,只好從自己的程式中找原因。

mso-ascii-font-family:"Times New Roman";mso-hansi-font-family:"Times New Roman"'>經過一陣調試,終於找到了引發異常的地方,是在建構函式中用DateTime.Now.Millisecondmso-ascii-font-family:"Times New Roman";mso-hansi-font-family:"Times New Roman"'>初始化隨機數的時候出錯了。看了半天,沒有找到為什麼出錯,只好祭出強大的Reflector"Times New Roman";mso-hansi-font-family:"Times New Roman"'>,然後動手寫了相應的C#mso-hansi-font-family:"Times New Roman"'>代碼:

color:blue;mso-font-kerning:0pt;mso-no-proof:yes'>privatemso-font-kerning:0pt;mso-no-proof:yes'> Random
rand = new Random(DateTime.Now.Millisecond);

mso-ascii-font-family:"Times New Roman";mso-hansi-font-family:"Times New Roman"'>用Reflector"Times New Roman";mso-hansi-font-family:"Times New Roman"'>查看系統產生的ILmso-hansi-font-family:"Times New Roman"'>代碼,發現是如下的語句:

新宋體;mso-hansi-font-family:"Times New Roman";mso-font-kerning:0pt;mso-no-proof:
yes'>.method public hidebysig specialname
rtspecialname instance void .ctor() cil
managed

新宋體;mso-hansi-font-family:"Times New Roman";mso-font-kerning:0pt;mso-no-proof:
yes'>{

新宋體;mso-hansi-font-family:"Times New Roman";mso-font-kerning:0pt;mso-no-proof:
yes'>    .maxstack 3

新宋體;mso-hansi-font-family:"Times New Roman";mso-font-kerning:0pt;mso-no-proof:
yes'>    .locals init (

新宋體;mso-hansi-font-family:"Times New Roman";mso-font-kerning:0pt;mso-no-proof:
yes'>        [0] valuetype
[mscorlib]System.DateTime CS$0$0000)

新宋體;mso-hansi-font-family:"Times New Roman";mso-font-kerning:0pt;mso-no-proof:
yes'>    L_0000: ldarg.0

新宋體;mso-hansi-font-family:"Times New Roman";mso-font-kerning:0pt;mso-no-proof:
yes'>    L_0001: call valuetype
[mscorlib]System.DateTime [mscorlib]System.DateTime::get_Now()

新宋體;mso-hansi-font-family:"Times New Roman";color:red;mso-font-kerning:0pt;
mso-no-proof:yes'>    L_0006: stloc.0

新宋體;mso-hansi-font-family:"Times New Roman";color:red;mso-font-kerning:0pt;
mso-no-proof:yes'>    L_0007: ldloca.s
CS$0$0000

新宋體;mso-hansi-font-family:"Times New Roman";mso-font-kerning:0pt;mso-no-proof:
yes'>    L_0009: call instance int32
[mscorlib]System.DateTime::get_Millisecond()

新宋體;mso-hansi-font-family:"Times New Roman";mso-font-kerning:0pt;mso-no-proof:
yes'>    L_000e: newobj instance void [mscorlib]System.Random::.ctor(int32)

新宋體;mso-hansi-font-family:"Times New Roman";mso-font-kerning:0pt;mso-no-proof:
yes'>    L_0013: stfld class [mscorlib]System.Random
EmitExamples.Animal::rand

新宋體;mso-hansi-font-family:"Times New Roman";mso-font-kerning:0pt;mso-no-proof:
yes'>    L_0018: ldarg.0

新宋體;mso-hansi-font-family:"Times New Roman";mso-font-kerning:0pt;mso-no-proof:
yes'>    L_0019: call instance void [mscorlib]System.Object::.ctor()

新宋體;mso-hansi-font-family:"Times New Roman";mso-font-kerning:0pt;mso-no-proof:
yes'>    L_001e: nop

新宋體;mso-hansi-font-family:"Times New Roman";mso-font-kerning:0pt;mso-no-proof:
yes'>    L_001f: ret

mso-font-kerning:0pt;mso-no-proof:yes'>}

mso-ascii-font-family:"Times New Roman";mso-hansi-font-family:"Times New Roman";
mso-bidi-font-family:"Times New Roman";mso-font-kerning:6.5pt;mso-ansi-language:
EN-US;mso-fareast-language:ZH-CN;mso-bidi-language:AR-SA'>其中我和它的區別就是少加了用紅色標出的兩句mso-fareast-font-family:宋體;mso-font-kerning:6.5pt;mso-ansi-language:EN-US;
mso-fareast-language:ZH-CN;mso-bidi-language:AR-SA'>IL"Times New Roman";mso-hansi-font-family:"Times New Roman";mso-bidi-font-family:
"Times New Roman";mso-font-kerning:6.5pt;mso-ansi-language:EN-US;mso-fareast-language:
ZH-CN;mso-bidi-language:AR-SA'>代碼。搗鼓了近一下午,只是大致的明白這是因為實值型別和參考型別的區別造成的,也記住了在載入參考型別時,需要使用mso-fareast-font-family:宋體;mso-font-kerning:6.5pt;mso-ansi-language:EN-US;
mso-fareast-language:ZH-CN;mso-bidi-language:AR-SA'>ldloca"Times New Roman";mso-hansi-font-family:"Times New Roman";mso-bidi-font-family:
"Times New Roman";mso-font-kerning:6.5pt;mso-ansi-language:EN-US;mso-fareast-language:
ZH-CN;mso-bidi-language:AR-SA'>指令,自己再用此方法寫了幾個例子,都沒有出問題。但是只是知其然而不知其所以然,在園子裡找了半天,也沒有發現有相關的文章,只好作罷,希望有知道的兄弟能夠在回複中給在下指出,我將萬分感激!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.