YamlDotNet icon indicating copy to clipboard operation
YamlDotNet copied to clipboard

Support the type creation of non-public parameterless constructor

Open VAllens opened this issue 3 years ago • 4 comments

Is your feature request related to a problem? Please describe. In order to comply with the design specification of domain model classes, we set the parameterless constructor to non-public. This makes it impossible for me to use the default deserializer.

Describe the solution you'd like We can refer to EF Core or System.Text.Json(.NET Core 3.1 and earlier versions). Consider types that support the creation of non-public parameterless constructor.

This is the implementation details: https://github.com/dotnet/corefx/blob/release/3.1/src/System.Text.Json/src/System/Text/Json/Serialization/ReflectionEmitMemberAccessor.cs FYI.

Describe alternatives you've considered I know that WithObjectFactory(...) method can be used

Additional context Add any other context or screenshots about the feature request here.

VAllens avatar Aug 01 '22 06:08 VAllens

Just to make sure I understand what you’re asking is for us to call a non public constructor when a public one does not exist? Shouldn’t be hard to do but I would make it an opt in feature.

EdwardCooke avatar Aug 06 '22 03:08 EdwardCooke

Yes, that's right. :)

Like this:

...
ConstructorInfo realMethod = type.GetConstructor(BindingFlags.Public | BindingFlags.NonPublic .....);
...

VAllens avatar Aug 08 '22 02:08 VAllens

Ok, I'll try and put something together in a little bit, might be a week or 2 though. Unless you want to take a stab at it.

EdwardCooke avatar Sep 09 '22 15:09 EdwardCooke

goooooooooooooooooood !!!

VAllens avatar Sep 13 '22 01:09 VAllens

You will be able to enable this feature by calling .EnablePrivateConstructors() on the deserializer builder.

EdwardCooke avatar Dec 09 '22 01:12 EdwardCooke

Cool !!!

VAllens avatar Dec 10 '22 09:12 VAllens