< Back

Get-Method

Mon Jan 13, 2020 8:24 pm

NAME Get-Method



SYNOPSIS

Gets methods of objects, even private methods.





SYNTAX

Get-Method -Type <Type> [[-Name] <String>] [[-BindingFlags] {Default | IgnoreCase | DeclaredOnly | Instance |

Static | Public | NonPublic | FlattenHierarchy | InvokeMethod | CreateInstance | GetField | SetField | GetProperty

| SetProperty | PutDispProperty | PutRefDispProperty | ExactBinding | SuppressChangeType | OptionalParamBinding |

IgnoreReturn}] [<CommonParameters>]



Get-Method -Type <Type> [[-Name] <String>] [-Public] [-NonPublic] [-Static] [-Instance] [<CommonParameters>]





DESCRIPTION

Get-Method retrieves methods of objects, even private methods.



The object sent to Get-Method is a Type object representing the class of objects to retrieve methods from. Binding

flags are used to differentiate between public and private, and static and instance using either the switch

parameters or an array of flags to the BindingFlags parameter.



It must be noted that when using flags, one of either public or private, and one of either static or instance must

both be used.





PARAMETERS

-Type <Type>

The type to retrieve methods from.



Required? true

Position? named

Default value

Accept pipeline input? true (ByValue)

Accept wildcard characters? false



-Name <String>

The name of the method to retrieve. Accepts wildcards.



Required? false

Position? 1

Default value

Accept pipeline input? false

Accept wildcard characters? false



-BindingFlags

The binding flags to be used when retrieving methods. The default is to retrieve public instance methods.



Required? false

Position? 2

Default value @('public','instance')

Accept pipeline input? false

Accept wildcard characters? false



-Public [<SwitchParameter>]

Specifies the Public binding flag.



Required? false

Position? named

Default value False

Accept pipeline input? false

Accept wildcard characters? false



-NonPublic [<SwitchParameter>]

Specifies the NonPublic binding flag, i.e. private.



Required? false

Position? named

Default value False

Accept pipeline input? false

Accept wildcard characters? false



-Static [<SwitchParameter>]

Specifies the Static binding flag.



Required? false

Position? named

Default value False

Accept pipeline input? false

Accept wildcard characters? false



-Instance [<SwitchParameter>]

Specifies the Instance binding flag.



Required? false

Position? named

Default value False

Accept pipeline input? false

Accept wildcard characters? false



<CommonParameters>

This cmdlet supports the common parameters: Verbose, Debug,

ErrorAction, ErrorVariable, WarningAction, WarningVariable,

OutBuffer, PipelineVariable, and OutVariable. For more information, see

about_CommonParameters (https:/go.microsoft.com/fwlink/?LinkID=113216).



INPUTS

System.Type





You can pipe System.Type objects to Get-Method.





OUTPUTS

System.Reflection.MethodInfo





Get-Method returns a MethodInfo object for each method retrieved.





NOTES





I created this originally to easily find the event methods used for WPF elements like add_Click on buttons. It

has since been updated for a more universal role.



Author: Chris Carter

Version: 1.0



-------------------------- EXAMPLE 1 --------------------------



PS C:\\>[System.Windows.Controls.Button] | Get-Method



This command will get the public instance methods (by default) for the WPF Button class. This is especially useful

for finding the available event methods on WPF elements like add_Click which Get-Member does not return.









-------------------------- EXAMPLE 2 --------------------------



PS C:\\>[Array] | Get-Method -NonPublic -Static



This command will get the private static methods for the Array class.









-------------------------- EXAMPLE 3 --------------------------



PS C:\\>[String] | Get-Method -BindingFlags 'Public','NonPublic','Static','Instance'



This command will get all the methods available to the String class.









-------------------------- EXAMPLE 4 --------------------------



PS C:\\>[System.Windows.Controls.Button] | Get-Method -Name add_Click



This command will get the method add_Click for the WPF Button class.









-------------------------- EXAMPLE 5 --------------------------



PS C:\\>[System.Windows.Controls.Button] | Get-Method -Name add_*



This command will get all methods that start with add_ for the WPF Button class.











RELATED LINKS