Csharp/C#教程:将本地语言exception转换为英语exception,包括英语堆栈跟踪分享


将本地语言exception转换为英语exception,包括英语堆栈跟踪

Currentlly我正在使用这种方法将exception转换为将原始exception消息转换为英语的exception:

C#:

[DebuggerStepThrough()] [Extension()] public Exception ToEnglish(T ex) where T : Exception { CultureInfo oldCI = Thread.CurrentThread.CurrentUICulture; Exception exEng = default(Exception); Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("en-US"); // Instance Exception one time to retrieve an English message. exEng = (Exception)Activator.CreateInstance(ex.GetType); // Instance Exception a second time to pass additional parameters to its constructor. exEng = (Exception)Activator.CreateInstance(ex.GetType, new object[] { exEng.Message, ex }); Thread.CurrentThread.CurrentUICulture = oldCI; return exEng; } //======================================================= //Service provided by Telerik (www.telerik.com) //Conversion powered by NRefactory. //======================================================= 

Vb.Net:

   Public Function ToEnglish(Of T As Exception)(ByVal ex As T) As Exception Dim oldCI As CultureInfo = Thread.CurrentThread.CurrentUICulture Dim exEng As Exception Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("en-US") ' Instance Exception one time to retrieve an English message. exEng = DirectCast(Activator.CreateInstance(ex.[GetType]), Exception) ' Instance Exception a second time to pass additional parameters to its constructor. exEng = DirectCast(Activator.CreateInstance(ex.[GetType], New Object() {exEng.Message, ex}), Exception) Thread.CurrentThread.CurrentUICulture = oldCI Return exEng End Function 

(我知道有些exception消息只是用英语部分定义)

但是,我对结果并不完全满意,因为我首先没有返回相同的运行时类型,而且结果exception的堆栈跟踪是空的,所以我需要保留原始exception的信息。生成的exception的InnerException属性。

然后,我想改进function,以满足那里的requeriments:

我不确定第2点是否可以实现,因为可能的堆栈限制?,无论如何我要知道,我怎么能做这两件事?

上述就是C#学习教程:将本地语言exception转换为英语exception,包括英语堆栈跟踪分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/955393.html

(0)
上一篇 2021年11月21日
下一篇 2021年11月21日

精彩推荐