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

Popular posts from this blog

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -