我最初将这个答案发布在这里,但是这里是转载,因为这不是完全相同的问题,但是答案相同:
FormatterServices.GetUninitializedObject()将创建实例而不调用构造函数。我通过使用Reflector并深入研究了一些核心.Net序列化类来找到此类。
我使用下面的示例代码对其进行了测试,看起来效果很好:
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Reflection;using System.Runtime.Serialization;namespace NoConstructorThingy{ class Program { static void Main(string[] args) { MyClass myClass = (MyClass)FormatterServices.GetUninitializedObject(typeof(MyClass)); //does not call ctor myClass.One = 1; Console.WriteLine(myClass.One); //write "1" Console.ReadKey(); } } public class MyClass { public MyClass() { Console.WriteLine("MyClass ctor called."); } public int One { get; set; } }}


