Embedding Managed Code C VB
suggest changeIntroduction
This topic is to briefly describe how C# or VB .NET Managed code can be scripted and utilised within a PowerShell script. This topic is not exploring all facets of the Add-Type cmdlet.
For more information on the Add-Type cmdlet, please refer to the MSDN documentation (for 5.1) here: https://msdn.microsoft.com/en-us/powershell/reference/5.1/microsoft.powershell.utility/add-type
Parameters
| Parameter | Details |
|---|---|
-TypeDefinition <String> |
The source code, as a string. |
-Language <String> |
Managed language. Accepted values include CSharp, CSharpVersion3, CSharpVersion2, VisualBasic, and JScript. Which values exist depends on the PowerShell version. |
Remarks
Removing Added types
Remove-TypeData removes extended type data (Update-TypeData, Types.ps1xml) from the session. It does not unload a class added with Add-Type. Those compiled types stay loaded until the session ends. See https://msdn.microsoft.com/en-us/powershell/reference/4.0/microsoft.powershell.utility/remove-typedata
CSharp and .NET syntax
Different C# language versions support different syntax.
Windows PowerShell 1.0, and -Language CSharpVersion2, use the C# 2 compiler. Generics are available (.NET 2.0). LINQ and lambda expressions need C# 3, and optional parameters need C# 4. Do not assume -Language CSharp means the newest compiler; on older Windows PowerShell it does not.