[CSHARP-70] Deserializing a public property with a private setter Created: 15/Oct/10  Updated: 20/Mar/14  Resolved: 27/Oct/10

Status: Closed
Project: C# Driver
Component/s: None
Affects Version/s: 0.5
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Justin Dearing Assignee: Craig Wilson
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

All



 Description   

A public property with a private setter will throw. E.g::

public IList<EventSession> Sessions

{ get; private set; }

Error is occurring inBsonPropertyMapper.cs line 226 because call to propertyInfo.GetSetmethod() is throwing.

public override Action<object, object> Setter {
get {
if (setter == null)

{ var setMethodInfo = propertyInfo.GetSetMethod(); // This throws var setMethodDelegate = (Action<TClass, TProperty>) Delegate.CreateDelegate(typeof(Action<TClass, TProperty>), setMethodInfo); setter = (obj, value) => setMethodDelegate((TClass) obj, (TProperty) value); }

return setter;
}
}



 Comments   
Comment by Craig Wilson [ 27/Oct/10 ]

fixed.

Comment by Steve Wagner [ 19/Oct/10 ]

Sorry wrong driver.

Comment by Justin Dearing [ 18/Oct/10 ]

Steve,

Thanks for the workaround, but are you against my patch?

Justin

Comment by Steve Wagner [ 18/Oct/10 ]

You can use a protected setter instead. Which will work with the current codebase.

Comment by Justin Dearing [ 16/Oct/10 ]

With same one line fix with unit tests:

http://github.com/zippy1981/mongo-csharp-driver/commit/5960cb6e0ada0c8df4ccf7f8471bccdce28def46

Comment by Justin Dearing [ 15/Oct/10 ]

One line fix:

var setMethodInfo = propertyInfo.GetSetMethod(true);

Quality is "works for me" but unit tests remain green

Generated at Wed Feb 07 21:35:43 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.