Get-Constructor Fun

PowerShell Team

Here is a quick and dirty function I wrote after getting PO’d at having to look up documentation for constructors:

NOTE:  Jim Truher thinks long typenames are useful so I added a switch (-FullName) so you could get them if you want them.

function get-Constructor ([type]$type, [Switch]$FullName)
{
    foreach ($c in $type.GetConstructors())
    {
        $type.Name + “(“
        foreach ($p in $c.GetParameters())
        {
             if ($fullName)
             {
                  “`t{0} {1},” -f $p.ParameterType.FullName, $p.Name 
             }else
             {
                  “`t{0} {1},” -f $p.ParameterType.Name, $p.Name 
             }
        }
        “)”
    }
}

PS> get-Constructor System.Windows.Thickness
Thickness(
        Double uniformLength,
)
Thickness(
        Double left,
        Double top,
        Double right,
        Double bottom,
)

PS>

PS> get-Constructor System.Datetime -FullName
DateTime(
 System.Int64 ticks,
)
DateTime(
 System.Int64 ticks,
 System.DateTimeKind kind,
)
DateTime(
 System.Int32 year,
 System.Int32 month,
 System.Int32 day,
)
DateTime(
 System.Int32 year,
 System.Int32 month,
 System.Int32 day,
 System.Globalization.Calendar calendar,
)
DateTime(
 System.Int32 year,
 System.Int32 month,
 System.Int32 day,
 System.Int32 hour,
 System.Int32 minute,
 System.Int32 second,
)
DateTime(
 System.Int32 year,
 System.Int32 month,
 System.Int32 day,
 System.Int32 hour,
 System.Int32 minute,
 System.Int32 second,
 System.DateTimeKind kind,
)
DateTime(
 System.Int32 year,
 System.Int32 month,
 System.Int32 day,
 System.Int32 hour,
 System.Int32 minute,
 System.Int32 second,
 System.Globalization.Calendar calendar,
)
DateTime(
 System.Int32 year,
 System.Int32 month,
 System.Int32 day,
 System.Int32 hour,
 System.Int32 minute,
 System.Int32 second,
 System.Int32 millisecond,
)
DateTime(
 System.Int32 year,
 System.Int32 month,
 System.Int32 day,
 System.Int32 hour,
 System.Int32 minute,
 System.Int32 second,
 System.Int32 millisecond,
 System.DateTimeKind kind,
)
DateTime(
 System.Int32 year,
 System.Int32 month,
 System.Int32 day,
 System.Int32 hour,
 System.Int32 minute,
 System.Int32 second,
 System.Int32 millisecond,
 System.Globalization.Calendar calendar,
)
DateTime(
 System.Int32 year,
 System.Int32 month,
 System.Int32 day,
 System.Int32 hour,
 System.Int32 minute,
 System.Int32 second,
 System.Int32 millisecond,
 System.Globalization.Calendar calendar,
 System.DateTimeKind kind,
)
Enjoy! 

Jeffrey Snover [MSFT]
Windows Management Partner Architect
Visit the Windows PowerShell Team blog at:    http://blogs.msdn.com/PowerShell
Visit the Windows PowerShell ScriptCenter at:  http://www.microsoft.com/technet/scriptcenter/hubs/msh.mspx

0 comments

Discussion is closed.

Feedback usabilla icon