解決 XslCompiledTransform 中的 StackOverflowException
在 Xsl Editor 開發過程中,呼叫 StackOverflowException
時遇到 XslCompiledTransform.Transform
可能會出現問題。此異常通常源自於無限遞歸的 Xsl 腳本,在轉換過程中壓垮了堆疊。
Microsoft 建議採取主動預防措施,而不是依賴 try-catch
區塊,因為這對於此特定異常無效。 Xsl 腳本本身內的計數器或狀態機制可以中斷遞歸循環,防止堆疊溢位。
但是,如果異常源自 .NET 內部方法,則需要替代策略:
XslTransform
。 這隔離了潛在的崩潰,允許主應用程式繼續並通知用戶失敗。 要將轉換隔離到單獨的進程中,請在主應用程式中使用以下程式碼:
<code class="language-csharp">// Example demonstrating argument passing in StartInfo Process p1 = new Process(); p1.StartInfo.FileName = "ApplyTransform.exe"; p1.StartInfo.UseShellExecute = false; p1.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; p1.Start(); p1.WaitForExit(); if (p1.ExitCode == 1) Console.WriteLine("A StackOverflowException occurred.");</code>
在單獨的進程(ApplyTransform.exe
)中,像這樣處理異常:
<code class="language-csharp">class Program { static void Main(string[] args) { AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; try { //Your XslTransform code here throw new StackOverflowException(); //Example - Replace with your actual transform code } catch (StackOverflowException) { Environment.Exit(1); } } static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) { if (e.IsTerminating) { Environment.Exit(1); } } }</code>
這個修改後的範例為處理 StackOverflowException
提供了更強大、更清晰的解決方案。 try-catch
中的 Main
區塊現在專門捕獲 StackOverflowException
和 UnhandledException
事件處理程序確保乾淨退出,防止出現「非法操作」對話方塊。 請記住將範例 throw new StackOverflowException();
替換為您實際的 Xsl 轉換程式碼。
以上是如何預防和處理 XslCompiledTransform 中的 StackOverflowException?的詳細內容。更多資訊請關注PHP中文網其他相關文章!