Welcome to MSDN Blogs Sign in | Join | Help

Get-Constructor Fun

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

Published Monday, September 01, 2008 10:53 PM by PowerShellTeam
Filed under:

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# Dew Drop - September 2, 2008 | Alvin Ashcraft's Morning Dew

Tuesday, September 02, 2008 8:47 AM by Dew Drop - September 2, 2008 | Alvin Ashcraft's Morning Dew

# re: Get-Constructor Fun

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,

)

Tuesday, September 02, 2008 1:12 PM by Jim Truher

Leave a Comment

(required) 
required 
(required) 
 
Page view tracker