从 C# DLL 打开控制台并使用它打印输出

Open console from C# DLL and print output using it

有一个 C# DLL 项目。它有一个 class 和一个 public 方法 Print()。 有没有可能从这个 DLL 打开控制台以显示文本? DLL 被第 3 方程序使用。

public class Logger // dll
{
    public void Print()
    {
        // Open Console
        // Standard output to console
    }
}

我找到了简单直接的解决方案 here。使用从 kernel32:

调用的 AllocConsole() 方法分配控制台

导入:

[DllImport("kernel32")]
static extern bool AllocConsole();

通话:

static void Main(string[] args) 
{
    //... magic happens here
    AllocConsole();
    Console.WriteLine("Hello!");
    //... continue magic
}