How do I determine the HResult for a System.IO.IOException?

前端 未结 6 1666
Happy的楠姐
Happy的楠姐 2020-12-01 05:02

The System.Exception.HResult property is protected. How can I peek inside an exception and get the HResult without resorting to reflection or other ugly hacks?


6条回答
  •  有刺的猬
    2020-12-01 05:58

    Have you profiled either of these cases? I'd imagine that the reflection method isn't all that slow, especially relative to all the other works your app will be doing and how often this exception will be likely to occur.

    If it turns out to be a bottleneck you can look into caching some of the reflection operations or generate dynamic IL to retrieve the property.

提交回复
热议问题