如何在.net CLR中在执行特定方法时禁用异常堆栈跟踪收集

本文关键字:异常 堆栈 跟踪 CLR net 执行 方法 | 更新日期: 2023-09-27 18:16:47

我有一个方法每秒被调用超过100次,由于它的内部实现,它经常抛出异常。

由于方法的内部实现,没有办法防止这些异常,但是我想减少这些异常的性能成本,例如通过禁用异常的堆栈跟踪收集,因为这些信息对我来说不方便。

你知道吗?

方法实现:

 public static TValue GetValue<TObj, TValue>(this TObj obj, Func<TObj, TValue> member, TValue defaultValueOnNull = default(TValue))
    {
        if (member == null)
            throw new ArgumentNullException("member");
        if (obj == null)
            throw new ArgumentNullException("obj");
        try
        {
            return member(obj); // We've lots of null reference exceptions here.
// And I'm not interested in stack trace of those exceptions, how to reduce performance costs here ?
            }
            catch (NullReferenceException)
            {
                return defaultValueOnNull;
            }
        } 

方法用法:

String flightNumber = originDestinationInformation.GetValue(dest => dest.OriginDestinationOption.FlighInfo.FlightNumber, "");

如何在.net CLR中在执行特定方法时禁用异常堆栈跟踪收集

你不可能不修改。net框架。