Replies: 3 comments
-
These are all compiler tricks with C#. There is nothing enforced at runtime. AutoMapper creates code at runtime that bypasses the compiler entirely. We could investigate raising an exception at configuration validation time, but that would involve us digging in to the compiler-emitted metadata (usually additional attributes on members), and I'm not sure it's worth the effort. |
Beta Was this translation helpful? Give feedback.
-
Actually, in your case, if you validate your configuration, you'll get an error, regardless of |
Beta Was this translation helpful? Give feedback.
-
I'm running into a similar issue when using collection marked with |
Beta Was this translation helpful? Give feedback.
-
C#11 / .NET 7 has a new feature that allows properties to be marked as required.
Documentation for it: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/required
This is also enforced during JSON deserialization: https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/required-properties
It seems that when using AutoMapper this isn't enforced though, and this code runs just fine:
It shouldn't be possible that
Address
is empty, ideally this would raise an exception when mapping.Beta Was this translation helpful? Give feedback.
All reactions