I've a method which is being called more than 100 times per second, and because of its internal implementation, it throws an exception often.
There's no way to prevent those exceptions because of method's internal implementation, but I'd like to reduce performance costs of these exceptions, for example by disabling stack trace gathering for exceptions, because those information are not come in handy for me.
Any idea ?
Method Implementation:
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;
}
}
Method usage :
String flightNumber = originDestinationInformation.GetValue(dest => dest.OriginDestinationOption.FlighInfo.FlightNumber, "");