C#程式集系列04,在程式集包含多個module的情境下理解關鍵字internal

來源:互聯網
上載者:User

標籤:style   blog   http   color   io   os   使用   ar   strong   

本篇在一個程式集包含多個module的情境下體驗internal的含義。


→查看F盤as檔案夾下的檔案

→刪除MainClass.exe

→把MyFirstModule和MySecondModule組裝到一個程式集中去

現在,MyDll.dll程式集中包含了2個module。
→反編譯,把MyDLL.dll的IL代碼顯示到3.txt檔案中

→開啟3.txt檔案

// Metadata version: v4.0.30319
.assembly extern mscorlib
{
  .publickeytoken = (B7 7A 5C 56 19 34 E0 89 )                         // .z\V.4..
  .ver 4:0:0:0
}
.assembly MyDll
{
  .custom instance void [mscorlib]System.Runtime.CompilerServices.RuntimeCompatibilityAttribute::.ctor() = ( 01 00 01 00 54 02 16 57 72 61 70 4E 6F 6E 45 78   // ....T..WrapNonEx
                                                                                                             63 65 70 74 69 6F 6E 54 68 72 6F 77 73 01 )       // ceptionThrows.
  .custom instance void [mscorlib]System.Runtime.CompilerServices.CompilationRelaxationsAttribute::.ctor(int32) = ( 01 00 08 00 00 00 00 00 ) 
  .hash algorithm 0x00008004
  .ver 0:0:0:0
}
.file MyFirstModule.netmodule
    .hash = (2F 9D 95 85 6E F4 D2 CA 50 61 C0 9F A1 58 C6 5F   // /...n...Pa...X._
             5D 1D 96 4B )                                     // ]..K
.file MySecondModule.netmodule
    .hash = (28 72 47 EE 2E 76 45 AA 6B 87 17 93 6B 4F 1C 5D   // (rG..vE.k...kO.]
             84 D8 07 CB ) 
.module MyDll.dll
// MVID: {DDC0023A-A0C5-4E28-8352-13288A2E427D}
.imagebase 0x10000000
.file alignment 0x00000200
.stackreserve 0x00100000
.subsystem 0x0003       // WINDOWS_CUI
.corflags 0x00000001    //  ILONLY
// Image base: 0x00400000
// *********** 反組譯碼完成 ***********************
// 警告: 建立了 Win32 資源檔 3.res

○ .assembly extern mscorlib,引用了.NET預設程式集
○ .assembly MyDll,表示MyDll程式集本身
○ .file MyFirstModule.netmodule,說明MyDll程式集引用了MyFirstModule這個module
○ .file MySecondModule.netmodule, 說明MyDll程式集引用了MySecondModule這個module
○ .module MyDll.dll,MyDll程式集包含了一個名稱為MyDll.dll的module

→重新編譯MainClass.cs檔案,並引用新產生的、包含2個module的MyDll.dll程式集

報錯了,為什嗎?
--因為在定義MyFristModule類和MySecondModule類的時候,雖然在class前面沒有修飾的關鍵字,但預設是internal
--internal意味著程式集的方法只能被該程式集調用,不能被外部程式集調用

 

當在MainClass.cs中調用MyFirstModule和MySecondModule的Hello方法

using System;
class MainClass
{
    static void Main()
    {
        MyFirstModule.Hello();
        MySecondModule.Hello();
    }
}

雖然MyFirstModule和MySecondModule已經被包含在MyDll.dll程式集了,但Hello方法不屬於MyDll.dll程式集,依然屬於MyFirstModule和MySecondModule。

 

總結:internal關鍵字修飾的類,其內部的方法只能被本程式集調用,不能被外部程式集調用。

 

“C#程式集系列”包括:

  C#程式集系列01,用記事本編寫C#,IL代碼,用DOS命令編譯器集,運行程式  C#程式集系列02,使用記事本查看可執行程式集的IL代碼  C#程式集系列03,引用多個module  C#程式集系列04,在程式集包含多個module的情境下理解關鍵字internal  C#程式集系列05,讓程式集包含多個module

 

參考資料:

http://www.computersciencevideos.org/  created by Jamie King

C#程式集系列04,在程式集包含多個module的情境下理解關鍵字internal

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.