RicoSuter / Namotion.Reflection

.NET library with advanced reflection APIs.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Question: how to get .ToContextualProperty().Nullability the same for emitted properties as for a coded ones

bessgeor opened this issue · comments

What am I trying to do?

Generate NRT-compatible classes using Reflection.Emit

What do I expect?

An emitted class having the same attributes as a coded one provides the same Nullability values for every property

What happens

Despite of having the very same attributes on emitted class, every emitted property is having the Unknown nullability while the coded ones are having the Nullable nullability.

I don't think this is a Namotion.Reflection bug, just trying to find someone to help me finding out what am I missing =)

Repro: https://github.com/bessgeor/namotion-reflection-emit-vs-roslyn (asserts the observed behavior).

Oh this might be a hard one, NRT is based on quite some attribue-magic :-)
Maybe this blog post helps?
https://blog.rsuter.com/the-output-of-nullable-reference-types-and-how-to-reflect-it/

Thank you for the great article, it has helped alot to understand the actual effect of NRT annotations.
The fun thing is, if the assertions on the attributes are correct, they match between the written and generated classes, from the field level to the assembly level (module and assembly levels are checked against attributes containing a "Null" in their names only, which is the most error-prone assertion, would check it tomorrow). But the Nullability is different.

You may check out the repro if you'd like (one file, 174 LoC including the emit part). I guess I'm missing something and I would re-check asm and module attributes, but if there's nothing, I'm stuck.

Thank you :)

Do you also generate the attribute classes directly in the assembly with the right namespace (ie not using them from another lib)?

I take them from the tests assembly, actually. Does this matter? I thought only attribute type shortname and parameters are important. Would try to emit them too and check it this way, thanks :)

Not sure but roslyn generates them per assembly I think.

As a reference i'd use sharplab and try to generate the same output (right side):
https://sharplab.io/#v2:EYLgtghglgdgNAExAagD4AEBMACAKgUwGcAXAWACgBvC7W7dAZnoEYAGAfmwDEB7H7SgHN8xANzZCI0QF8KNOo3oAWbACEIAJwAU6Np2AQAXnBatswAK6GAlNnm1q5e9lnlXQA==

I think Namotion.Reflection doesnt care whether the attribute is generated in the assembly or just referenced, not sure however if roslyn analyzers work as expected...