共用方式為


/moduleassemblyname (為模組指定 Friend 組件) (C# 編譯器選項)

更新:2007 年 11 月

指定 .netmodule 能夠存取非公用型別的組件。

/moduleassemblyname:assembly_name

引數

  • assembly_name
    .netmodule 能夠存取非公用型別的組件名稱。

備註

當建置 .netmodule 並且下列條件為真時應該使用 /moduleassemblyname

  • .netmodule 需要存取現有組件中的非公用型別

  • 您知道 .netmodule 將建置在其中的組件名稱

  • 現有組件已授與 friend 組件存取權給 .netmodule 將建置在其中的組件

如需建置 .netmodule 的詳細資訊,請參閱 /target:module (建立加入至組件的模組) (C# 編譯器選項)

如需 friend 組件的詳細資訊,請參閱 Friend 組件 (C# 程式設計手冊)

這個選項在開發環境內無法使用;只有從命令列進行編譯時才能使用。

在 Visual Studio 中無法使用這個編譯器選項,而且無法利用程式設計的方式變更它。

範例

這個範例會使用私用型別建置組件,並且讓 friend 組件存取名為 csman_an_assembly 的組件。

// moduleassemblyname_1.cs
// compile with: /target:library
using System;
using System.Runtime.CompilerServices;

[assembly:InternalsVisibleTo ("csman_an_assembly")]

class An_Internal_Class 
{
    public void Test() 
    { 
        Console.WriteLine("An_Internal_Class.Test called"); 
    }
}

這個範例會建置存取組件 moduleassemblyname_1.dll 中非公用型別的 .netmodule。因為知道這個 .netmodule 將建置在名為 csman_an_assembly 的組件中,所以能夠指定 /moduleassemblyname 以便讓 .netmodule 存取已授與 friend 組件存取 csman_an_assembly 權限之組件中的非公用型別。

// moduleassemblyname_2.cs
// compile with: /moduleassemblyname:csman_an_assembly /target:module /reference:moduleassemblyname_1.dll
class B {
    public void Test() {
        An_Internal_Class x = new An_Internal_Class();
        x.Test();
    }
}

這個程式碼範例會參考之前建置的組件和 .netmodule 建置組件 csman_an_assembly。

// csman_an_assembly.cs
// compile with: /addmodule:moduleassemblyname_2.netmodule /reference:moduleassemblyname_1.dll
class A {
    public static void Main() {
        B bb = new B();
        bb.Test();
    }
}

An_Internal_Class.Test called

請參閱

其他資源

C# 編譯器選項

專案屬性 (Visual Studio)