CsvHelper icon indicating copy to clipboard operation
CsvHelper copied to clipboard

Calling Map and AutoMap from ClassMap constructor triggers ReSharper warnings

Open ColbyODonnell opened this issue 3 years ago • 3 comments

The documentation (see these 5 examples) ...

  • https://joshclose.github.io/CsvHelper/getting-started/
  • https://joshclose.github.io/CsvHelper/examples/configuration/class-maps/auto-mapping/
  • https://joshclose.github.io/CsvHelper/examples/configuration/class-maps/inline-type-conversion/
  • https://joshclose.github.io/CsvHelper/examples/configuration/class-maps/optional-maps/
  • https://joshclose.github.io/CsvHelper/examples/configuration/class-maps/validation/

... illustrates calling Map and AutoMap from the constructor of a derived class of ClassMap<T>. Doing so immediately triggers a warning from ReSharper: Virtual member call in constructor, which is explained here: https://www.jetbrains.com/help/resharper/2022.3/VirtualMemberCallInConstructor.html

This design seems inherently flawed. Is there any alternative way (perhaps from a non-constructor method) to apply mapping that won't trigger a scary warning about virtual members being called from constructors? I would prefer not to pretend this is not a problem by suppressing the warning.

ColbyODonnell avatar Dec 15 '22 17:12 ColbyODonnell

Did you make your class sealed? I believe you won't get that warning if you do.

JoshClose avatar Dec 15 '22 21:12 JoshClose

Bam, that solves it. Thank you!

P.S. Should one or more of the examples used sealed?

ColbyODonnell avatar Dec 15 '22 23:12 ColbyODonnell

All ClassMap implementations should probably have it in the documentation.

JoshClose avatar Dec 15 '22 23:12 JoshClose