共用方式為


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

若要探索全域程式集緩存中 .NET Framework 元件的完整名稱,請使用全域程式集緩存工具 (Gacutil.exe)。 請參閱 如何:檢視全域程式集緩存的內容

對於 .NET Core 元件,以及不在全域程式集緩存中的 .NET Framework 元件,您可以透過數種方式取得完整元件名稱:

  • 您可以使用程式代碼將資訊輸出至控制台或變數,也可以使用 Ildasm.exe (IL 反組譯程式) 來檢查元件元數據,其中包含完整名稱。

  • 如果應用程式已經載入元件,您可以擷取Assembly.FullName屬性的值以取得完整名稱。 您可以使用在該元件中定義的 AssemblyType 屬性來擷取 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;
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

另請參閱