共用方式為


Assembly Class

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Represents an assembly, which is a reusable, versionable, and self-describing building block of a common language runtime application.

Inheritance Hierarchy

System.Object
  System.Reflection.Assembly
    System.Reflection.Emit.AssemblyBuilder

Namespace:  System.Reflection
Assembly:  mscorlib (in mscorlib.dll)

Syntax

'Declaration
<ClassInterfaceAttribute(ClassInterfaceType.None)> _
<ComVisibleAttribute(True)> _
Public MustInherit Class Assembly _
    Implements ICustomAttributeProvider
[ClassInterfaceAttribute(ClassInterfaceType.None)]
[ComVisibleAttribute(true)]
public abstract class Assembly : ICustomAttributeProvider

The Assembly type exposes the following members.

Constructors

  Name Description
Protected method Assembly

Top

Properties

  Name Description
Public property CodeBase Gets the location of the assembly as specified originally, for example, in an AssemblyName object.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 EntryPoint Gets the entry point of this assembly.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 FullName Gets the display name of the assembly.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 ImageRuntimeVersion Gets a string representing the version of the common language runtime (CLR) saved in the file containing the manifest.
Public property IsDynamic Gets a value that indicates whether the current assembly was generated dynamically in the current process by using reflection emit.
Public property Location Gets the path or UNC location of the loaded file that contains the manifest.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 ManifestModule Gets the module that contains the manifest for the current assembly.

Top

Methods

  Name Description
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 CreateInstance Locates the specified type from this assembly and creates an instance of it using the system activator, using case-sensitive search.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)

In Silverlight for Windows Phone, this member is overridden by Equals(Object).

In XNA Framework, this member is overridden by Equals(Object).
Protected methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 Finalize Allows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 GetCallingAssembly Returns the Assembly of the method that invoked the currently executing method.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 GetCustomAttributes(Boolean) Gets all the custom attributes for this assembly.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 GetCustomAttributes(Type, Boolean) Gets the custom attributes for this assembly as specified by type.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 GetExecutingAssembly Gets the assembly that contains the code that is currently executing.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 GetExportedTypes Gets the types defined in this assembly that are visible outside the assembly.
Public method GetFile Security Critical. Gets a FileStream for the specified file in the file table of the manifest of this assembly.
Public method GetFiles Security Critical. Gets the files in the file table of an assembly manifest, specifying whether to include resource modules.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)

In Silverlight for Windows Phone, this member is overridden by GetHashCode().

In XNA Framework, this member is overridden by GetHashCode().
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 GetManifestResourceNames Returns the names of all the resources in this assembly.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 GetManifestResourceStream(String) Loads the specified manifest resource from this assembly.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 GetManifestResourceStream(Type, String) Loads the specified manifest resource, scoped by the namespace of the specified type, from this assembly.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 GetModules Gets all the modules that are part of this assembly.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 GetName() Security Critical. Gets an AssemblyName for this assembly.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 GetName(Boolean) Security Critical. Gets an AssemblyName for this assembly, setting the codebase as specified by copiedName.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 GetSatelliteAssembly(CultureInfo) Gets the satellite assembly for the specified culture.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 GetSatelliteAssembly(CultureInfo, Version) Gets the specified version of the satellite assembly for the specified culture.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 GetType() Gets the Type of the current instance. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 GetType(String) Gets the Type object with the specified name in the assembly instance.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 GetType(String, Boolean) Gets the Type object with the specified name in the assembly instance and optionally throws an exception if the type is not found.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 GetTypes Gets the types defined in this assembly.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 IsDefined Indicates whether or not a specified attribute has been applied to the assembly.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 Load(AssemblyName) Security Critical. Loads an assembly given its AssemblyName.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 Load(String) Loads an assembly given the long form of its name.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 LoadFrom Security Critical. Loads an assembly given its file name or path.
Protected methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 ToString Returns the full name of the assembly, also known as the display name. (Overrides Object.ToString().)

Top

Remarks

Use the Assembly class to load assemblies, to explore the metadata and constituent parts of assemblies, to discover the types contained in assemblies, and to create instances of those types.

To load assemblies dynamically, the Assembly class provides the static Load(String) method overload (Shared method overload in Visual Basic). Assemblies are loaded into the Silverlight application domain. Other overloads can be called only by the trusted assemblies in the .NET Framework.

To get an Assembly object for the currently executing assembly, use the GetExecutingAssembly method.

Many members of the Assembly class provide information about an assembly. For example:

  • The GetName method returns an AssemblyName object that provides access to the parts of the assembly display name.

  • The GetCustomAttributes method lists the attributes applied to the assembly.

  • The GetFiles method provides access to the files in the assembly manifest.

  • The GetManifestResourceNames method provides the names of the resources in the assembly manifest.

The GetTypes method lists all the types in the assembly. The GetExportedTypes method lists the types that are visible to callers outside the assembly. The GetType method can be used to search for a particular type in the assembly. The CreateInstance method can be used to search for and create instances of types in the assembly.

Examples

The following code example shows how to obtain the currently executing assembly, create an instance of a type contained in that assembly, and invoke one of the type's methods with late binding. For this purpose, the code example defines a class named Example, with a method named SampleMethod. The constructor of the class accepts an integer, which is used to compute the return value of the method.

The code example also demonstrates the use of the GetName method to obtain an AssemblyName object that can be used to parse the full name of the assembly. The example displays the version number of the assembly and the EntryPoint property.

Imports System.Reflection
Imports System.Security.Permissions

<Assembly: AssemblyVersionAttribute("1.0.2000.0")> 

Public Class Example
   Private Shared outputBlock As System.Windows.Controls.TextBlock
   Private factor As Integer

   Public Sub New()
      Me.New(42)
   End Sub
   Public Sub New(ByVal f As Integer)
      factor = f
   End Sub

   Public Function SampleMethod(ByVal x As Integer) As Integer
      outputBlock.Text &= String.Format(vbCrLf & "Example.SampleMethod({0}) executes.", x) & vbCrLf
      Return x * factor
   End Function

   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)

      Example.outputBlock = outputBlock
      Dim assem As [Assembly] = [Assembly].GetExecutingAssembly()

      outputBlock.Text &= "Assembly Full Name:" & vbCrLf
      outputBlock.Text &= assem.FullName & vbCrLf

      ' Create an object from the assembly, using the default constructor.
      Dim o As Object = assem.CreateInstance("SilverlightApplication.Example")

      ' Make a late-bound call to an instance method of the object.  
      Dim m As MethodInfo = o.GetType().GetMethod("SampleMethod")
      Dim ret As Object = m.Invoke(o, New Object() {42})
      outputBlock.Text &= String.Format("SampleMethod returned {0}.", ret) & vbCrLf

      outputBlock.Text &= vbCrLf & "Assembly entry point:" & vbCrLf
      Dim entry As MethodInfo = assem.EntryPoint
      If entry Is Nothing Then
         outputBlock.Text &= "No entry point defined for the assembly." & vbCrLf
      Else
         outputBlock.Text &= entry.ToString() & vbCrLf
      End If
   End Sub
End Class

' This code example produces output similar to the following:
'
'Assembly Full Name:
'source, Version=1.0.2000.0, Culture=neutral, PublicKeyToken=null
'
'Example.SampleMethod(42) executes.
'SampleMethod returned 1784.
'
'Assembly entry point:
'No entry point defined for the assembly.
' 
using System;
using System.Reflection;
using System.Security.Permissions;

[assembly: AssemblyVersionAttribute("1.0.2000.0")]

public class Example
{
   private static System.Windows.Controls.TextBlock outputBlock;
   private int factor;

   public Example() : this(42) {}
   public Example(int f)
   {
      factor = f;
   }

   public int SampleMethod(int x)
   {
      outputBlock.Text += String.Format("\nExample.SampleMethod({0}) executes.", x) + "\n";
      return x * factor;
   }

   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {

      Example.outputBlock = outputBlock;
      Assembly assem = Assembly.GetExecutingAssembly();

      outputBlock.Text += "Assembly Full Name:" + "\n";
      outputBlock.Text += assem.FullName + "\n";

      // Create an object from the assembly, using the default constructor.
      Object o = assem.CreateInstance("Example");

      // Make a late-bound call to an instance method of the object.    
      MethodInfo m = assem.GetType("Example").GetMethod("SampleMethod");
      Object ret = m.Invoke(o, new Object[] { 42 });
      outputBlock.Text += String.Format("SampleMethod returned {0}.\n", ret);

      outputBlock.Text += "\nAssembly entry point:\n";
      MethodInfo entry = assem.EntryPoint;
      if (entry == null)
      {
         outputBlock.Text += "No entry point defined for the assembly.\n";
      }
      else
      {
         outputBlock.Text += entry.ToString() + "\n";
      }
   }
}

/* This code example produces output similar to the following:

Assembly Full Name:
source, Version=1.0.2000.0, Culture=neutral, PublicKeyToken=null

Example.SampleMethod(42) executes.
SampleMethod returned 1784.

Assembly entry point:
No entry point defined for the assembly.
 */

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Thread Safety

This type is thread safe.