Fixed bug that would cause a struct to not match and throw a runtime error.
When a struct had an empty constructor that modified a field/property to a non-default value. The matcher would fail and throw a runtime error. This bug fix attempts to fix that issue by essentially zero initializing the struct the same way that default(T) does through the use of a runtime helper method. Simply, we replace Activator.CreateInstance with GetUninitializedObject.
The idea came from this post
Which contains and deprecated method as of .NET8. Microsoft has a recommended fix for that here.
This resolves the issue in #766
Thanks for the pull request!
Could you please add some unit tests?
I added a simple test that does fail on the main branch. Not sure there is a whole lot to test here. I did ensure that all tests pass, especially ones that take in nullable primitive types like int? as they need special handling with the new DefaultInstanceOfValueType functionality. If you have any specific scenarios you would like to see let me know.
Is there anything still blocking this from being merged?
I fixed the format issue.
Is there anything still blocking this from being merged?
Only a review I guess 😅
@304NotModified Anything still holding this back that I can help with? I would love to not have to maintain my own branch anymore and it seems that others have faced problems similar to this one.
cc @304NotModified
@304NotModified I am sure you are busy, is there anything I can help with that is holding you back from approving these changes?
Hey!
I'm not that active anymore on NSubstitute 😅 And I got hunderds of notifications/tags recently, so dropped a lot of those notifications 😅
I could try to check this PR this week, but I'm not sure if this needs an approval of me?
reviewed. There is still an open discussion in this PR. I don't know the exact side effects of this change, so unfortunately I cannot approve this.
@dtchepak maybe enable the check of the discussions so we could not forget this
Under settings -> rules -> rulesets