如何:尋找組件的完整名稱

若要在全域組件快取中找到 .NET Framework 組件的完整名稱,請使用全域組件快取工具 (Gacutil.exe)。 請參閱如何:檢視全域組件快取的內容

對於 .NET Core 元件,以及不在全域組件快取中的 .NET Framework 元件,可透過多種方式取得完整組件名稱:

  • 您可以使用程式碼輸出資訊至主控台或至變數,或者您可使用 Ildasm.exe (IL 反組譯工具) 以檢查組件中繼資料,其中包含完整名稱。

  • 如果應用程式已經載入組件時,您可以擷取 Assembly.FullName 屬性的值來取得完整名稱。 您可以使用該元件中定義的 TypeAssembly 屬性來擷取物件 Assembly 的參考。 這個範例將提供說明。

  • 如果您知道組件的檔案系統路徑,您可以呼叫 static (C#) 或 Shared (Visual Basic) AssemblyName.GetAssemblyName 方法來取得完整組件名稱。 以下是一個簡單的範例。

    using System;
    using System.Reflection;
    
    public class Example
    {
       public static void Main()
       {
          Console.WriteLine(AssemblyName.GetAssemblyName(@".\UtilityLibrary.dll"));
       }
    }
    // The example displays output like the following:
    //   UtilityLibrary, Version=1.1.0.0, Culture=neutral, PublicKeyToken=null
    
    Imports System.Reflection
    
    Public Module Example
       Public Sub Main
          Console.WriteLine(AssemblyName.GetAssemblyName(".\UtilityLibrary.dll"))
       End Sub
    End Module
    ' The example displays output like the following:
    '   UtilityLibrary, Version=1.1.0.0, Culture=neutral, PublicKeyToken=null
    
  • 您可以使用 Ildasm.exe (IL 反組譯工具) 來檢查組件的中繼資料,其中包含完整名稱。

如需有關設定組件屬性的詳細資訊,如版本、文化特性及組件名稱,請參閱設定組件屬性。 如需有關給予組件強式名稱的詳細資訊,請參閱建立和使用強式名稱的組件

範例

下列範例示範如何顯示含有指定類別組件的完整名稱至主控台。 它會使用 Type.Assembly 屬性,從該組件中定義的型別擷取組件的參考。

#using <System.dll>
#using <System.Data.dll>

using namespace System;
using namespace System::Reflection;

ref class asmname
{
public:
    static void Main()
    {
        Type^ t = System::Data::DataSet::typeid;
        String^ s = t->Assembly->FullName->ToString();
        Console::WriteLine("The fully qualified assembly name " +
            "containing the specified class is {0}.", s);
    }
};

int main()
{
    asmname::Main();
}
using System;
using System.Reflection;

class asmname
{
    public static void Main()
    {
        Type t = typeof(System.Data.DataSet);
        string s = t.Assembly.FullName.ToString();
        Console.WriteLine("The fully qualified assembly name " +
            "containing the specified class is {0}.", s);
    }
}
Imports System.Reflection

Class asmname
    Public Shared Sub Main()
        Dim t As Type = GetType(System.Data.DataSet)
        Dim s As String = t.Assembly.FullName.ToString()
        Console.WriteLine("The fully qualified assembly name " +
            "containing the specified class is {0}.", s)
    End Sub
End Class

另請參閱