C# serialize a class without a parameterless constructor

后端 未结 5 1910
予麋鹿
予麋鹿 2020-12-18 21:08

I\'m implementing a factory pattern for 3 different cryptography classes. The factory will determine which one to create and then get a serialized instance of the correct cl

5条回答
  •  粉色の甜心
    2020-12-18 21:40

    For anyone who is still looking a few years later, the answer is now FormatterServices.GetUninitializedObject. Be careful with it, as this is just a piece of memory set to all nulls and zeros and if a field in the object's default value is something other than zero, it might be set wrong: "Because the new instance of the object is initialized to zero and no constructors are run, the object might not represent a state that is regarded as valid by that object."

提交回复
热议问题