RicoSuter / Namotion.Reflection

.NET library with advanced reflection APIs.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

`EnsureValidNullability` doesn't work with records

maxkoshevoi opened this issue · comments

I'm trying to use EnsureValidNullability in my project, but it always crashes on any record with:

System.InvalidOperationException: 'Method may only be called on a Type for which Type.IsGenericParameter is true.'

@RicoSuter could you please take a look at this?

Preliminary investigation suggests this issue is caused by recursively checking nullability of the compiler generated EqualityContract property on record objects.

#126 has a tentative fix (no tests...) that ignores all properties with CompilerGeneratedAttribute. This makes EnsureValidNullability and ValidateNullability usable on records. I'm not 100% sure if this is entirely backwards compatible, but it seems to me checking nullability on compiler generated properties is unlikely what users want/need.

For funsies I pulled down the code in that PR and tested it against some of my records and I still ran into that error.

FWIW, the PR fixed it for me.