Welcome to Typemock Answers. Here you can ask and receive answers from other community members. And if you liked or disliked an answer or thread: react with an up- or downvote Enjoy!

Is there a way to set a { get; private set; } property?

+3 votes
Is there a way to set a { get; private set; } property? I am aware of PrivateObject.SetProperty() but am wondering if there is an Isolator way to do this.

Object.SetField() does not do it:

[ img ]
asked Nov 12, 2009 by Neil (9,200 points)

3 Answers

+1 vote
Hi Neil,

ObjectState is designed to control non-public fields, rather than properties. Isolator can control oroperties are just like methods, so you can use Isolator's non-public API:
A a = new A();
Isolate.NonPublic.Property.WhenGetCalled(a, "A").WillReturn(10);


Hope this helps,
Doron
Typemock Support
answered Nov 12, 2009 by doron (26,680 points)
+1 vote
Neil,

I just noticed that your property is in fact public - the API I showed in my previous post is the non-public API. It will still work, but you can use the type-safe public API instead:
A a = new A();
Isolate.WhenCalled(() => a.X).WillReturn(10);


Doron
Typemock Support
answered Nov 12, 2009 by doron (26,680 points)
+3 votes
Hi doron,

I had forgotten about WhenCalled().WillReturn(). Thanks.
answered Nov 12, 2009 by Neil (9,200 points)
...