Creating Your own Types

Share |

Join PowerShell.com!
Subscribe to Feed

PowerShell eBook
Sign up for
Your PowerTip of the Day:

Award-winning PowerShellPlus


Admin Guide to PowerShell Remoting


Mastering PowerShell eBook

Did you know that you can compile any .NET source code on the fly and use this to create your own types? Here is an example illustrating how to create a new type from c# code that has both static and dynamic methods:

$source = @'
public class Calculator
{
public static int Add(int a, int b)
{
return (a + b);
}

public int Multiply(int a, int b)
{
return (a * b);
}
}
'@
Add-Type -TypeDefinition $source
[Calculator]
[Calculator]::Add(5,10)
$myCalculator = New-Object Calculator
$myCalculator.Multiply(3,12)

Posted Jun 28 2010, 08:00 AM by ps1
Copyright 2012 PowerShell.com. All rights reserved.