I'm playing with a script that parses strings to Intergers using system.int32.parse() and using the system.globalization.numbersytles enum (see http://msdn.microsoft.com/en-us/library/system.globalization.numberstyles.aspx).
With C# you can specify a FlagsAttribute atrribute to, in effect, convert the enum values into bit patterns that you can add up. Using this attribute, [system.globalization.numberstyles]::AllowTrainingWhite is 0x0002, etc.
How do you do this in PowerShell?
Thomas
To answer my own question - easy. Just use the .valule__ field.
Basically, you can have an enum such as:
[System.Globalization.NumberStyles]::AllowLeadingSign, which outputs as "AllowLeadingSign". However in some cases, you can "or" a set of enums to tell some .NET method to use all the values in doing whatever it does. For example, the parse() method on Int32 (and other integers). This method uses several of the numberstyle enum values to tell parse how to parse the number.
In C# this might look like this:
num = " -45 ";val = int.Parse(num, NumberStyles.AllowLeadingSign | NumberStyles.AllowLeadingWhite | NumberStyles.AllowTrailingWhite);
However in PowerShell, it's not quite as simple, but not that hard either. Turns out each Enum value has a .value__ property. The [System.Globalization.NumberStyles]::AllowLeadingSign.value outputs as 4. So to convert this c# code to Powershell you could do something like:
$parsing = [System.Globalization.NumberStyles]::AllowLeadingSign.value__ + [System.Globalization.NumberStyles]::AllowLeadingWhite.value__ + [System.Globalization.NumberStyles]::AllowTrailingWhite.value__ $val = [system.int32]::Parse($num,$parsing)
The PowerShell script is not as 'short' as the C# - but you could easiliy make it shorter by putting the calculation "inside" the Parse() call. I left it out for clarity.
Thanks to Shay Levy for the tip!