次の方法で共有


Windows PowerShell and .NET Together (creating the object)

By this time, you are probably aware of the fact that the Windows PowerShell integrates with the Microsoft .NET Framework very well and leverages it power fully. So, if you are new to PowerShell and don’t know much about PowerShell scripting, you can still go ahead and use its power by relying on the old .NET framework (which I suppose you are very good at :))

Here is the code snippet, which demonstrates how to create class, object and functions using C# and executes in Windows PowerShell:

=========================================================

Add-Type @'

public class Calculator
{
    public double AddNumber(double x, double y) {
        return x + y;
    }

    public double SubtractNumber(double x, double y) {
        return x - y;
    }

    public double MultiplyNumber(double x, double y) {
        return x * y;
    }

    public double DivideNumber(double x, double y) {
        return x / y;
    }
}
'@

$dblX = read-host "Enter the first number"
$dblY = read-host "Enter the second number"

$objCalculator = New-Object Calculator
write-host "Sum-->" $objCalculator.AddNumber($dblX,$dblY)
write-host "Difference-->" $objCalculator.SubtractNumber($dblX,$dblY)
write-host "Multiplication-->" $objCalculator.MultiplyNumber($dblX,$dblY)
write-host "Division-->" $objCalculator.DivideNumber($dblX,$dblY)

=========================================================

Copy the above code in a file and save it with an extension “ps1” e.g. “Calculator.ps1” and then run it from the PowerShell command prompt.

Happy Programming!

Comments