Assembly.GetAssembly Method
.NET Framework 4
Gets the currently loaded assembly in which the specified class is defined.
Assembly: mscorlib (in mscorlib.dll)
Parameters
- type
- Type: System.Type
An object representing a class in the assembly that will be returned.
| Exception | Condition |
|---|---|
| ArgumentNullException |
type is null. |
In order to call this method, you must have a Type object, which means that the assembly in which the class is defined must already be loaded.
The following example retrieves the assembly of a specified class.
Assembly SampleAssembly; // Instantiate a target object. Int32 Integer1 = new Int32(); Type Type1; // Set the Type instance to the target class type. Type1 = Integer1.GetType(); // Instantiate an Assembly class to the assembly housing the Integer type. SampleAssembly = Assembly.GetAssembly(Integer1.GetType()); // Gets the location of the assembly using file: protocol. Console.WriteLine("CodeBase=" + SampleAssembly.CodeBase);
-
ReflectionPermission
when invoked late-bound through mechanisms such as Type.InvokeMember. Associated enumeration: ReflectionPermissionFlag.MemberAccess
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Sample Using PowerShell
<#
.SYNOPSIS
This script demonstrates the Assembly.GetType method.
.DESCRIPTION
This script creates an int32, then calls into .NET to find
the assembly that the int32 class comes from. The assembly
details are then output. This script is a re-work of an MSDN
code sample.
.NOTES
File Name : Get-AssemblyDetails.ps1
Author : Thomas Lee - tfl@psp.co.uk
Requires : PowerShell Version 2.0
.LINK
This script posted to:
http://www.pshscripts.blogspot.com
MSDN Sample posted at:
http://msdn.microsoft.com/en-us/library/system.reflection.assembly.getassembly.aspx
.EXAMPLE
PSH [C:\foo]: .\Get-AssemblyDetails.ps1'
CodeBase=: file:///C:/Windows/Microsoft.NET/Framework64/v2.0.50727/mscorlib.dll
#>
# Set the Type instance to the target class type.
$Integer1 = New-Object System.Int32
$Type1 = $Integer1.GetType();
# Instantiate an Assembly class to the assembly housing the Integer type.
$SampleAssembly = [System.Reflection.Assembly]::GetAssembly($Integer1.GetType())
# Gets the location of the assembly using file: protocol.
"CodeBase=: {0}" -f $SampleAssembly.CodeBase
- 7/14/2010
- Thomas Lee