Assembly.GetExecutingAssembly 方法

定義

取得組件,其中含有目前正在執行的程式碼。

public:
 static System::Reflection::Assembly ^ GetExecutingAssembly();
public static System.Reflection.Assembly GetExecutingAssembly ();
static member GetExecutingAssembly : unit -> System.Reflection.Assembly
Public Shared Function GetExecutingAssembly () As Assembly

傳回

包含目前正在執行之程式碼的組件。

範例

下列範例會 Type.Assembly 使用 屬性,根據該元件中包含的類型,取得目前正在執行的元件。 它也會呼叫 GetExecutingAssembly 方法,以顯示它傳回 Assembly 代表相同元件的物件。

using namespace System;
using namespace System::Reflection;

ref class Example
{};

void main()
{
   // Get the assembly from a known type in that assembly.
   Type^ t = Example::typeid;
   Assembly^ assemFromType = t->Assembly;
   Console::WriteLine("Assembly that contains Example:");
   Console::WriteLine("   {0}\n", assemFromType->FullName);

   // Get the currently executing assembly.
   Assembly^ currentAssem = Assembly::GetExecutingAssembly();
   Console::WriteLine("Currently executing assembly:");
   Console::WriteLine("   {0}\n", currentAssem->FullName);

   Console::WriteLine("The two Assembly objects are equal: {0}",
                      assemFromType->Equals(currentAssem));
}
// The example displays the following output:
//    Assembly that contains Example:
//       GetExecutingAssembly1, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
//
//    Currently executing assembly:
//       GetExecutingAssembly1, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
//
//    The two Assembly objects are equal: True
using System;
using System.Reflection;

class Example
{
   static void Main()
   {
      // Get the assembly from a known type in that assembly.
      Type t = typeof(Example);
      Assembly assemFromType = t.Assembly;
      Console.WriteLine("Assembly that contains Example:");
      Console.WriteLine("   {0}\n", assemFromType.FullName);

      // Get the currently executing assembly.
      Assembly currentAssem = Assembly.GetExecutingAssembly();
      Console.WriteLine("Currently executing assembly:");
      Console.WriteLine("   {0}\n", currentAssem.FullName);

      Console.WriteLine("The two Assembly objects are equal: {0}",
                        assemFromType.Equals(currentAssem));
   }
}
// The example displays the following output:
//    Assembly that contains Example:
//       GetExecutingAssembly1, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
//
//    Currently executing assembly:
//       GetExecutingAssembly1, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
//
//    The two Assembly objects are equal: True
Imports System.Reflection

Public Module Example
   Public Sub Main()
      ' Get the assembly from a known type in that assembly.
      Dim t As Type = GetType(Example)
      Dim assemFromType As Assembly = t.Assembly
      Console.WriteLine("Assembly that contains Example:")
      Console.WriteLine("   {0}", assemFromType.FullName)
      Console.WriteLine()
      
      ' Get the currently executing assembly.
      Dim currentAssem As Assembly = Assembly.GetExecutingAssembly()
      Console.WriteLine("Currently executing assembly:")
      Console.WriteLine("   {0}", currentAssem.FullName)
      Console.WriteLine()
      
      Console.WriteLine("The two Assembly objects are equal: {0}",
                        assemFromType.Equals(currentAssem))
   End Sub
End Module
' The example displays the following output:
'    Assembly that contains Example:
'       GetExecutingAssembly1, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
'
'    Currently executing assembly:
'       GetExecutingAssembly1, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
'
'    The two Assembly objects are equal: True

備註

基於效能考慮,只有在設計時間不知道目前執行的元件時,才應該呼叫這個方法。 擷取 Assembly 代表目前元件之對象的建議方法是使用 Type.Assembly 元件中找到之類型的 屬性,如下列範例所示。

using System;
using System.Reflection;

public class Example
{
   public static void Main()
   {
      Assembly assem = typeof(Example).Assembly;
      Console.WriteLine("Assembly name: {0}", assem.FullName);
   }
}
// The example displays output like the following:
//    Assembly name: Assembly1, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
Imports System.Reflection

Module Example
   Public Sub Main()
      Dim assem As Assembly = GetType(Example).Assembly
      Console.WriteLine("Assembly name: {0}", assem.FullName)
   End Sub
End Module
' The example displays the following output:
'   Assembly name: Assembly1, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null

若要取得包含呼叫目前執行程式碼之 方法的元件,請使用 GetCallingAssembly

適用於