Can I write a class using powershell?

后端 未结 3 1052
北恋
北恋 2020-12-13 00:01

I\'m still learning powershell, and so far I haven\'t found an answer on this website, despite a few searches. With Powershell being built on top of the .NET framework, can

3条回答
  •  眼角桃花
    2020-12-13 00:44

    Take a look at Add-Type cmdlet. It lets you write C# and other code in PowerShell. For example (from the above link),

    C:\PS>$source = @"
    public class BasicTest
    {
        public static int Add(int a, int b)
        {
            return (a + b);
        }
    
        public int Multiply(int a, int b)
        {
            return (a * b);
        }
    }
    "@
    
    C:\PS> Add-Type -TypeDefinition $source
    
    C:\PS> [BasicTest]::Add(4, 3)
    
    C:\PS> $basicTestObject = New-Object BasicTest 
    C:\PS> $basicTestObject.Multiply(5, 2)
    

提交回复
热议问题