Advertisement
Guest User

Untitled

a guest
Dec 7th, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 KB | None | 0 0
  1. public class DomainException : Exception { }
  2.  
  3. public class Foo
  4. {
  5. public void Throw()
  6. {
  7. throw new DomainException();
  8. }
  9. }
  10.  
  11. public static class FooManager
  12. {
  13. public static void InvokeThrow(object foo)
  14. {
  15. var methodInfo = typeof(Foo).GetMethod("Throw");
  16. methodInfo.Invoke(foo, null);
  17. }
  18. }
  19.  
  20. try
  21. {
  22. FooManager.InvokeThrow(new Foo());
  23. }
  24. catch(DomainException ex)
  25. {
  26. //Do something cool.
  27. }
  28.  
  29. public static void InvokeThrow(object foo)
  30. {
  31. try
  32. {
  33. var methodInfo = typeof(Foo).GetMethod("Throw");
  34. methodInfo.Invoke(foo, null);
  35. }
  36. catch(TargetInvocationException ex)
  37. {
  38. throw ex.InnerException;
  39. }
  40. }
  41.  
  42. try
  43. {
  44. var methodInfo = typeof(Foo).GetMethod("Throw");
  45. methodInfo.Invoke(foo, null);
  46. }
  47. catch(TargetInvocationException ex)
  48. {
  49. ExceptionDispatchInfo.Capture(ex.InnerException).Throw();
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement