谁能告诉我为什么下面的代码没有在 PS 屏幕上显示“blablabla”?我所看到的只是从函数返回的随机数。
# Create a function that loads our managed code into powershell
function InitType
{
[string]$SourceCode = @"
using System;
namespace TestApp
{
// This class houses the public methods that we'll use from powershell
public static class TestMethods
{
public static int GetRandom()
{
var id = new Random().Next();
Console.WriteLine("bla bla bla");
return id;
}
}
}
"@
# use the powershell 2.0 add-type cmdlet to compile the source and make it available to our powershell session
add-type -TypeDefinition $SourceCode
}
# Load our C# code
InitType
# Call our method
[TestApp.TestMethods]::GetRandomAndOutputMessage
()