c# - Restrict usage of parameterless constructor to serialization/Activator/new() -
i've had following question in mind , couldn't find question on so:
how can have default constructor serialization/activator purposes, while making sure consumer discouraged/disabled using it?
in past i've used hints like
///<summary> /// not use default constructor ///</summary>
which overlooked easily, unless hover , check every class use.
while better visual indication this:
[obsolete("do not use default constructor")]
it complete abuse of feature leaves me shivering.
is there common way i'm not aware of deal this, or me feels annoyed in first place?
if class within class library, , serialization happens class library itself, can create internal
wrapper class (deriving original class) exposes serialization constructor. in way, can make sure nothing outside calling constructor. approach, have use wrapper type when deserializing.
that still mean constructor can called inside class library of course, prevents of unintended use.
Comments
Post a Comment