Create class in powershell

11/10/2012 22:06

 

#Define class with properties
$source = @"
public class TaskItem
{
    private string title;
    public string Title
    {
        get {return title;}
        set {title=value;}
    }
}
"@
 
Add-Type -TypeDefinition $source
 
$o = New-Object TaskItem
 $o.Title="til"
 $o

 

C:\PS>$source = @"
public class BasicTest
{
    public static int Add(int a, int b)
    {
        return (a + b);
    }

    public int Multiply(int a, int b)
    {
        return (a * b);
    }
}
"@

C:\PS> Add-Type -TypeDefinition $source

C:\PS> [BasicTest]::Add(4, 3)

C:\PS> $basicTestObject = New-Object BasicTest 
C:\PS> $basicTestObject.Multiply(5, 2)