Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在执行特定方法时禁用.NET CLR中的异常堆栈跟踪收集?_C#_.net_Performance_Exception Handling_Clr - Fatal编程技术网

C# 如何在执行特定方法时禁用.NET CLR中的异常堆栈跟踪收集?

C# 如何在执行特定方法时禁用.NET CLR中的异常堆栈跟踪收集?,c#,.net,performance,exception-handling,clr,C#,.net,Performance,Exception Handling,Clr,我有一个每秒被调用100次以上的方法,由于其内部实现,它经常抛出异常 由于方法的内部实现,无法防止这些异常,但我希望通过禁用异常的堆栈跟踪收集来降低这些异常的性能成本,因为这些信息对我来说并不方便 有什么想法吗 方法实现: public static TValue GetValue<TObj, TValue>(this TObj obj, Func<TObj, TValue> member, TValue defaultValueOnNull = default(TVa

我有一个每秒被调用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;
            }
        } 

好吧,如果不修改.NET framework,你就无法做到这一点。

NullReferenceException
是你必须防止捕捉不到的东西。如果有一些神奇的标志来做到这一点,系统仍然需要一路走到各个堆栈框架,执行展开等操作,才能找到神奇的标志。就性能而言,这不会节省太多。@SriramSakthivel是的,你是对的,但我使用这种方法来获得更清晰的代码,而不需要对对象进行大量的“if not null”检查。@YasserMoradi很抱歉地说:你的评论是荒谬的。如果您不想检查null使用,则不应该捕获
NullReferenceException
。IMHO捕获
NullReferenceException
的代码是垃圾。那么为什么要检查
if(member==null)
if(obj==null)
呢?你可以消除它,得到更好的代码,不是吗?首先是对问题的相关回答/评论。我认为没有办法满足我的要求。
String flightNumber = originDestinationInformation.GetValue(dest => dest.OriginDestinationOption.FlighInfo.FlightNumber, "");