PoshCode Logo PowerShell Code Repository

Reflection by Joel Bennett 22 months ago (modification of post by Joel Bennett view diff)
View followups from Joel Bennett | diff | embed code: <script type="text/javascript" src="http://PoshCode.org/embed/1711"></script>download | new post

Helpers for working with .Net classes: Get-Constructor, Get-Assembly, Add-Assembly, Get-Type

  1. #requires -version 2.0
  2. #History:
  3. # 1.0  - First public release (March 19, 2010)
  4.  
  5. function Get-Type {
  6.     <#
  7.     .Synopsis
  8.         Gets the types that are currenty loaded in .NET,
  9.         or gets information about a specific type
  10.     .Description
  11.         Gets all of the loaded types, or gets the possible values for an
  12.         enumerated type or value.
  13.     .Example
  14.         # Gets all loaded types
  15.         Get-Type
  16.     .Example
  17.         # Gets types from System.Management.Automation
  18.         Get-Type -Assembly ([PSObject].Assembly)
  19.     .Example
  20.         # Gets all of the possible values for the ApartmentState property
  21.         [Threading.Thread]::CurrentThread.ApartmentState | Get-Type
  22.     .Example
  23.         # Gets all of the possible values for an apartmentstate
  24.         [Threading.ApartmentState] | Get-Type
  25.     #>
  26.     [CmdletBinding(DefaultParameterSetName="Assembly")]  
  27.     param(
  28.     # The assembly to collect types from
  29.     [Parameter(ParameterSetName="Assembly", ValueFromPipeline=$true, Position=0)]
  30.     [Reflection.Assembly[]]$Assembly
  31. ,
  32.     # The enumerated value to get all of the possibilties of
  33.     [Parameter(ParameterSetName="Enum", ValueFromPipeline=$true, Position=0)]
  34.     [Enum]$Enum
  35. ,
  36.     # Returns possible values if the Type was an enumerated value
  37.     # Otherwise, returns the static members of the type
  38.     [Parameter(ParameterSetName="Type", ValueFromPipeline=$true, Position=0)]
  39.     [Type[]]$Type
  40.     )
  41.  
  42.     process {
  43.         switch ($psCmdlet.ParameterSetName) {
  44.             Assembly {
  45.                 if (! $psBoundParameters.Count -and ! $args.Count) {
  46.                     $Assembly = [AppDomain]::CurrentDomain.GetAssemblies()
  47.                 }
  48.                 foreach ($asm in $assembly) {
  49.                     if ($asm) { $asm.GetTypes() }  
  50.                 }
  51.             }
  52.             Type {
  53.                 foreach ($t in $type) {
  54.                     if ($t.IsEnum) {
  55.                         [Enum]::GetValues($t)
  56.                     } else {
  57.                         $t  | Get-Member -static
  58.                     }                
  59.                 }
  60.             }
  61.             Enum {
  62.                 [Enum]::GetValues($enum.GetType())        
  63.             }
  64.        }
  65.     }
  66. }
  67.  
  68. function Add-Assembly {
  69. #.Synopsis
  70. #  Load assemblies
  71. #.Description
  72. #  Load assemblies from a folder
  73. #.Parameter Path
  74. #  Specifies a path to one or more locations. Wildcards are permitted. The default location is the current directory (.).
  75. #.Parameter Passthru
  76. #  Returns System.Runtime objects that represent the types that were added. By default, this cmdlet does not generate any output.
  77. #  Aliased to -Types
  78. #.Parameter Recurse
  79. #  Gets the items in the specified locations and in all child items of the locations.
  80. #
  81. #  Recurse works only when the path points to a container that has child items, such as C:\Windows or C:\Windows\*, and not when it points to items that do not have child items, such as C:\Windows\*.dll
  82. param(
  83.    [Parameter(ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true, Position=0)]
  84.    [Alias("PSPath")]
  85.    [string[]]$Path = "."
  86. ,
  87.    [Alias("Types")]
  88.    [Switch]$Passthru
  89. ,
  90.    [Switch]$Recurse
  91. )
  92. process {
  93.    foreach($file in Get-ChildItem $Path -Filter *.dll -Recurse:$Recurse) {
  94.       Add-Type -Path $file.FullName -Passthru:$Passthru | Where { $_.IsPublic }
  95.    }
  96. }
  97. }
  98.  
  99.  
  100. function Get-Assembly {
  101. <#
  102. .Synopsis
  103.    Get a list of assemblies available in the runspace
  104. .Description
  105.    Returns AssemblyInfo for all the assemblies available in the current AppDomain, optionally filtered by partial name match
  106. .Parameter Name
  107.    A regex to filter the returned assemblies. This is matched against the .FullName of the assembly.
  108. #>
  109. param(
  110.    [Parameter(ValueFromPipeline=$true, Position=0)]
  111.    [string]$Name = ''
  112. )
  113. process {
  114.    [appdomain]::CurrentDomain.GetAssemblies() |? {$_.FullName -match $Name}
  115. }
  116. }
  117.  
  118. Set-Alias gasm Get-Assembly
  119.  
  120. function Get-Constructor {
  121. <#
  122. .Synopsis
  123.    Get a list of constructors for a type
  124. .Description
  125.    Returns AssemblyInfo for all the assemblies available in the current AppDomain, optionally filtered by partial name match
  126. .Parameter Name
  127.    A regex to filter the returned assemblies. This is matched against the .FullName of the assembly.
  128. #>
  129. param(
  130.    [Parameter(ValueFromPipeline=$true, Position=0)]
  131.    [Type]$type
  132. )
  133. process {
  134.    $type.GetConstructors() |
  135.       Format-Table @{
  136.          l="$($type.Name) Constructors"
  137.          e={ ($_.GetParameters() | % { $_.ToString() }) -Join ", " }
  138.       }
  139. }
  140. }
  141.  
  142. Set-Alias gctor Get-Constructor

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me