我知道这是一个非常粗糙的解决方案,但如果你需要一些 PHP 代码,而不想每次都重复移植到 C# 中,可以尝试以下方法,尽管这意味着你需要在目标机器上安装 PHP 命令行工具。
第一步是编写一个 PHP 脚本,不断从 stdin 读取数据,使用供应商提供的特殊类进行解码,并将结果写入 stdout。下面是一个非常简单的示例:
<?php
include("VendorDecodingClass.php");
while(true)
{
$input = fgets(STDIN);
if($input)
{
$output = VendorDecoding::decode($input);
fwrite(STDOUT, $output);
}
}
?>
无论如何,一旦您安装了该软件包,在C#应用程序中,在开始时创建一个新的进程来运行该脚本,然后将Process实例保存在某个地方,以便稍后可以引用STDIN和STDOUT。示例:
ProcessStartInfo procStartInfo = new ProcessStartInfo("php", "yourscript.php")
procStartInfo.RedirectStandardOutput = true
procStartInfo.UseShellExecute = false
procStartInfo.CreateNoWindow = true
Process proc = new Process()
proc.StartInfo = procStartInfo
proc.Start()
当您想要解码数据时,只需向您创建的php进程的stdin写入,并在stdout上等待响应。使用stdin / stdout方法比每次想要解码一些数据时创建新进程更有效,因为创建该进程的开销可能会很明显。
proc.StandardInput.WriteLine(somedata); //somedata is whatever you want to decode
//may need to wait here, or perhaps catch an exception on the next line?
String result = proc.StandardOutput.ReadLine();
//now result should contain the result of the decoding process
免责声明,我没有测试过这些代码,但那是我可能会使用的一般要点。
我还想到了另外一件事,你需要一些机制来终止PHP进程。也许使用Process.Kill
可以,但如果解码执行任何文件IO或其他重要操作,则可能需要以某种方式发送中断信号给php脚本。