Get-Constructor Fun

Get-Constructor Fun

Rate This
  • Comments 2

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

Leave a Comment
  • Please add 7 and 5 and type the answer here:
  • Post
  • PingBack from http://www.alvinashcraft.com/2008/09/02/dew-drop-september-2-2008/

  • sometimes, it's good to know the fullname of the parameter type (since you might not know that Calendar is actually System.Globalization.Calendar.  Changing $p.ParameterType.Name to $p.ParameterType.FullName gives you that!

    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,

    )

Page 1 of 1 (2 items)