I'd like to cast a .NET object to another .NET type, but:
- The target .NET type (class) is stored in a variable
- I don't want to use the
-asPowerShell operator - I am using complex, non-primitive types
How would you achieve this?
For example, this is the "PowerShell" way to do it, but I don't want to use -as:
$TargetType = [System.String]; # The type I want to cast to
1 -as $TargetType; # Cast object as $TargetType
Unfortunately, this does not work:
$TargetType = [System.String];
[$TargetType]1;
.. because PowerShell does not allow the use of variables inside the square brackets, in this scenario.
I am imagining something like:
$TargetType = [System.String];
$TargetType.Cast(1); # Does something like this exist in the .NET framework?
Can it be done with .NET method syntax? Is there a static method that does this?