Loading a .cs
file directly from PowerShell. This was taken from Knuckle-Dragger's SO answer.
There is no compile of .cs
code, just edit the -Path
argument to point to your .cs file. I also had to add -Raw
when I tried to do said thing outside of this exmaple.
PowerShell file
$source = Get-Content -Path "A:\basic.cs" -Raw
Add-Type -TypeDefinition "$source"
# Call a static method
[BasicTest]::Add(4, 3)
# Create an instance and call an instance method
$basicTestObject = New-Object BasicTest
$basicTestObject.Multiply(5, 2)
Basic.cs
public class BasicTest
{
public static int Add(int a, int b)
{ return (a + b);
}
public int Multiply(int a, int b)
{
return (a * b);
}
}