1

I have a class :

public class Foo {
// here so serializer can materialize the class
public Foo(){

}

public Foo(string bar){
    ImportantStuff = bar;
}

public string ImportantStuff { get; set;}
public string OtherBits{ get; set;}
public int eresting { get; set;}

}

How can i protect ImportantStuff being set by anyone except the serializer (json.net/ EF) ?

Nikola Sivkov
  • 2,812
  • 3
  • 37
  • 63

1 Answers1

2

You can place access modifiers on property accessors.

[JsonProperty]
public string ImportantStuff { get; private set; }
//                                    ^^^

If you really must prevent Foo members from changing it, use a private setter AND move the property to a base class. But I interpreted the question to mean that you don't want consumers of the class to be able to set it.

The JsonProperty attribute is required because JSON.net will default to skipping properties with non-public setter. See Private setters in Json.Net and http://daniel.wertheim.se/2010/11/06/json-net-private-setters/

(Naturally other code that runs with full permission can do anything it wants to the insides of your object, if the right hoops are jumped through)

Community
  • 1
  • 1
Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
  • 2
    @AmitJoki: Serializers use reflection and bypass permissions. Ordinary clients of the class will be restricted by the permission. There's not much that can be done to restrict other members of the same class. – Ben Voigt Apr 18 '14 at 16:19
  • @BenVoigt, you are right i don't want anyone outside the class to be able to set it and yet serializers to be able, i will test with private and accept the answer – Nikola Sivkov Apr 18 '14 at 16:24
  • Yes, you are right, I've but not as much as you have. But I'm quite sure that I'll be more informed on this when I get to your age ;) – Amit Joki Apr 18 '14 at 16:25
  • @Amit: There is nothing wrong with disagreeing with experts, it's a great way to learn, and sometimes even experts are wrong. But since you aren't expert (yet), you should phrase your comments with the possibility that you are wrong. For example, your first comment could have been "Are you sure this allows only JSON and EF to set the property? It looks to me like members of the class will also be able to make changes." – Ben Voigt Apr 18 '14 at 16:29
  • deleted that comment. Will be more careful when commenting next time – Amit Joki Apr 18 '14 at 16:31
  • Any classes that inherits from Foo cannot set ImportantStuff like so. – Francis.Beauchamp Apr 18 '14 at 16:45
  • @Francis.Beauchamp: Allowing subclasses to set `ImportantStuff` appears to be undesirable. The question says it should be resettable only by serializers. – Ben Voigt Apr 18 '14 at 16:49
  • @Aviatrix: Can you provide an explanation of "didn't work"? Didn't compile? Exception in serializer? Serializer ran but data was missing? – Ben Voigt Apr 18 '14 at 16:52
  • @BenVoigt Serializer ran fine, but data was "null", i've also tried other access modifiers, same result – Nikola Sivkov Apr 18 '14 at 16:54
  • @Aviatrix: So it works with `public` setter and only with `public`? – Ben Voigt Apr 18 '14 at 17:04
  • @BenVoigt Yes. Only public works, data is there. private/protected/internal = no data. – Nikola Sivkov Apr 18 '14 at 17:07
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/50980/discussion-between-aviatrix-and-ben-voigt) – Nikola Sivkov Apr 18 '14 at 17:07
  • my uh, not to expert answer would be include and if statement in the setter – DidIReallyWriteThat Apr 18 '14 at 17:09