RickStrahl / WestwindToolkit

A utility toolkit for .NET development from Core to Web
273 stars 124 forks source link

ReflectionUtils.SetPropertInternal tries to write to ReadOnly properties #21

Closed robvon closed 4 years ago

robvon commented 4 years ago

Current Code: Line 547 in ReflectionUtils or thereabouts:

if (Member.MemberType == MemberTypes.Property) ((PropertyInfo)Member). pi.SetValue(Parent, Value, null); else ((FieldInfo)Member).SetValue(Parent, Value);

perhaps a test: ((PropertyInfo)Member).CanWrite ?? before the SetValue

It's done that way in the StringSerializer

This issue also occurs in the "ConfigurationFileConfigurationProvider" when missingFields are set.

Regards

Rob v N Brisbane, AUS

RickStrahl commented 4 years ago

Thanks Rob. This library is no longer maintained here. It's been moved to Westwind.Utilities here:

https://github.com/RickStrahl/Westwind.Utilities

Looks like that problem still exists there as well. Fixing now.

                // Get the member
                MemberInfo Member = Parent.GetType().GetMember(PureProperty, ReflectionUtils.MemberAccess)[0];
                if (Member.MemberType == MemberTypes.Property)
                {
                    var prop = (PropertyInfo) Member;
                    if (prop.CanWrite)
                        prop.SetValue(Parent, Value, null);
                }
                else
                    ((FieldInfo)Member).SetValue(Parent, Value);
robvon commented 4 years ago

Thanks mate. Not much wind out there today eh!

On Tue, 28 Jan. 2020, 5:19 pm Rick Strahl, notifications@github.com wrote:

Thanks Rob. This library is no longer maintained here. It's been moved to Westwind.Utilities here:

https://github.com/RickStrahl/Westwind.Utilities

Looks like that problem still exists there as well. Fixing now.

// Get the member MemberInfo Member = Parent.GetType().GetMember(PureProperty, ReflectionUtils.MemberAccess)[0]; if (Member.MemberType == MemberTypes.Property) { var prop = (PropertyInfo) Member; if (prop.CanWrite) prop.SetValue(Parent, Value, null); } else ((FieldInfo)Member).SetValue(Parent, Value);

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/RickStrahl/WestwindToolkit/issues/21?email_source=notifications&email_token=AAHWKRYG4KQLYHRMYO6GQODQ77MBVA5CNFSM4KMK43P2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEKCJDPA#issuecomment-579113404, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAHWKR7WMJOLZAMJJ6JBOULQ77MBVANCNFSM4KMK43PQ .