replaysMike / AnyClone

A CSharp library that can deep clone any object using only reflection.
MIT License
47 stars 7 forks source link

Cloning static readonly fails in .Net Core 3.0 #11

Closed andyesys closed 3 years ago

andyesys commented 4 years ago

I tried to clone a NodaTime.Period and it throws this exception: System.FieldAccessException: Cannot set initonly static field '<Zero>k__BackingField' after type 'NodaTime.Period' is initialized.

Related to https://github.com/dotnet/standard/issues/1598

andyesys commented 4 years ago

I can clone a NodaTime.Period by ignoring the Zero Property with periods.Clone(nameof(Period.Zero)).

But I think not cloning readonly statics should be the default behaviour.

replaysMike commented 4 years ago

I see this as well, and I’m pretty sure I know the cause as it’s affecting multiple features. It’s due to a change in the .net core runtime support for static read only fields.

I will work on a workaround for this right away.

On Thu, Jul 30, 2020 at 4:59 AM Andreas Schönebeck notifications@github.com wrote:

I tried to clone a NodaTime.Period and it throws this exception: "System.FieldAccessException: Cannot set initonly static field 'k__BackingField' after type 'NodaTime.Period' is initialized."

Related to dotnet/standard#1598 https://github.com/dotnet/standard/issues/1598

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/replaysMike/AnyClone/issues/11, or unsubscribe https://github.com/notifications/unsubscribe-auth/AATJ54SLST5S6NEYQ64G2GTR6FOCJANCNFSM4PNSLHKA .

replaysMike commented 3 years ago

This issue is now closed. I was able to get around this limitation (.net core 3+ reflection) by utilizing some IL to override accessibility of those values.