Create object instance without invoking constructor?

后端 未结 11 1950
不知归路
不知归路 2020-11-28 05:01

In C#, is there a way to instantiate an instance of a class without invoking its constructor?

Assume the class is public and is defined in a 3rd par

11条回答
  •  迷失自我
    2020-11-28 05:40

    I have not tried this, but there is a method called FormatterServices.GetUninitializedObject that is used during deserialization.

    Remarks from MSDN says:

    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.

提交回复
热议问题