Powershell generate System.Type out of String

Viewed 89

I am dynamically generating .net objects of a certain type like this $MyType=([System.Type]::GetType("System.String") This works fine for standard .net objects.

I now created a custom class like this

class InstanceName
{
    [String] hidden $Val
    InstanceName([String]$CVal)
    {
        $this.Val=$CVal
    }
}

Here, this method does not work as powershell cannot find the type. $MyType=([System.Type]::GetType("InstanceName")

Any idea how I can get a System.Type of a custom PS class?

Thx!

1 Answers

Use the -as type conversion operator:

$instanceNameType = 'InstanceName' -as [type]

# test the type reference
$instanceNameType::new("")
Related