0

I'm trying to write a unit test which has to throw a specific SqlException with Number 1205 or 1222. After fallowing this (because SqlException is sealed) now I have to use reflection to set the Number property which is read only. I've tried using Mono.reflection but using the fallowing code I get an exception:

typeof(SqlException).GetProperty("Number").GetBackingField()
            .SetValue(exception, SqlDeadlockViolation);
Exception: "Value does not fall within the expected range."

Any idea on how I can set the Numer property?

  • 2
    Maybe look at this https://stackoverflow.com/questions/11976996/moq-and-throwing-a-sqlexception – Timothy G. May 21 '21 at 13:06
  • It sure does. Thank you. Though the constructor signature may change over time and you'll need to patch the code. I was wondering why it's not possible to get the backing field of the Number property ? – uesr466093 May 25 '21 at 09:20

0 Answers0