Archive for the “Add-Type” Category

We can use C#  quite easily with the Add-Type Cmdlet. With the -language parameter, you can also use VB .NET. However,if you are into functional programming and like F#, you can use that as well, although not quite as easily.

First, go and download the September 2008 CTP of F#. Once you have this installed, launch the 32 bit version of PowerShell. I’m using a 64 bit Vista machine and I had problems with the 64 bit version of Powershell with one line which I will get to in a bit. I think it has to do with the F# CodeDom Provider, not with PowerShell itself.

The F# CodeDom.dll was installed in C:\Program Files (x86)\FSharp-1.9.2.9\bin for me. Your mileage may vary. Anyway, cd into the bin directory of F# and you will find a file called FSharp.Compiler.CodeDom.dll. Once you are there, you can run the following lines to load up the F# Code Provider.

Add-Type -Path FSharp.Compiler.CodeDom.dll
$provider = New-Object Microsoft.FSharp.Compiler.CodeDom.FSharpCodeProvider
$fsharpCode = @"
let sample = [1;2;3;4;5;6;7]
"@
$fsharpType = Add-Type -TypeDefinition $fSharpCode -CodeDomProvider $provider -PassThru |
where { $_.IsPublic }
$fsharpType::sample

When you call Sample on $fsharpType, it will return the array with numbers 1 through 7.

For some reason when I ran this on a 64 bit I get the following error when I try to add the type with typeDefinition $fsharpCode on line 6

Add-Type : The system cannot find the file specified
At line:6 char:23
+ $fsharpType = Add-Type <<<<  -TypeDefinition $fSharpCode -CodeDomProvider $provider -PassThru | where { $_.IsPublic }
    + CategoryInfo          : NotSpecified: (:) [Add-Type], Win32Exception
    + FullyQualifiedErrorId : System.ComponentModel.Win32Exception,Microsoft.PowerShell.Commands.AddTypeCommand

If you are into this sort of thing, have fun!

Comments 4 Comments »

There may be, on some occassion, a need to create a class with a set of properties in PowerShell. This can’t be done natively, but it can be done with some very simple C# syntax, thanks to C# version 3.0, which is really a new compiler as opposed to a new version of .NET. The base class library is the same. Here is a very basic class with four properties.

 

   1: PS C:\Users\andy.schneider> $csharp = @"
   2: >> public class Andy
   3: >> {
   4: >>  public int age {get;set;}
   5: >>  public string firstName {get;set;}
   6: >>  public string lastName {get;set;}
   7: >>  public string blog {get;set;}
   8: >> }
   9: >> "@
  10: >>
  11: PS C:\Users\andy.schneider>

But we still have to get it into PowerShell. We can use the Add-Type Cmdlet to do this, but it will fail unless we use the C# 3.0 compiler under the covers. We can specify the language we are using with the –Language parameter. I know CSharp30 is wrong, but I like to give it garbage so it will tell me what the valid languages are. Because it takes an ENUM we can find out that CsharpVersion3 is a valid language.

 

   1: PS C:\Users\andy.schneider>
   2: PS C:\Users\andy.schneider> add-type $csharp -Language Csharp30
   3: Add-Type : Cannot bind parameter 'Language'. Cannot convert value "Csharp30" to type "Microsoft.PowerShell.Commands.Lan
   4: guage" due to invalid enumeration values. Specify one of the following enumeration values and try again. The possible e
   5: numeration values are "CSharp, CSharpVersion3, VisualBasic, JScript".
   6: At line:1 char:27
   7: + add-type $csharp -Language <<<<  Csharp30
   8: PS C:\Users\andy.schneider> add-type $csharp -Language CsharpVersion3

All right, now we are ready to to after running the command on line 8.

We can create a new object of type Andy and set it to $andy

   1: PS C:\Users\andy.schneider> $andy = new-object andy
   2: PS C:\Users\andy.schneider> $andy | gm
   3:  
   4:  
   5:    TypeName: Andy
   6:  
   7: Name        MemberType Definition
   8: ----        ---------- ----------
   9: Equals      Method     System.Boolean Equals(Object obj)
  10: GetHashCode Method     System.Int32 GetHashCode()
  11: GetType     Method     System.Type GetType()
  12: ToString    Method     System.String ToString()
  13: age         Property   System.Int32 age {get;set;}
  14: blog        Property   System.String blog {get;set;}
  15: firstName   Property   System.String firstName {get;set;}
  16: lastName    Property   System.String lastName {get;set;}

Now we have a full blown object with properties and we can create new instances of them all day long, and the C# was pretty darn straight forward.

Comments 1 Comment »