object System.Runtime.Serialization.IObjectReference.GetRealObject(System.Runtime.Serialization.StreamingContext context) { MySingletonClass realObject = Instance; realObject.Merge(this); return(realObject); }
public void ShouldCreateJustOneInstance() { MySingletonClass first = MySingletonClass.Instance; MySingletonClass second = MySingletonClass.Instance; Assert.Same(first, second); first.SomeValue++; Assert.Equal(first.SomeValue, second.SomeValue); second.SomeValue++; Assert.Equal(first.SomeValue, second.SomeValue); }
public MySingletonClass CreateInstance() { if (currentInstance == null) { currentInstance = new MySingletonClass(); } else { return(currentInstance); } }
static void Main(string[] args) { MySingletonClass r1 = MySingletonClass.GetInstance(); Console.WriteLine("R1 value = {0}", r1.ValueGetter()); r1.ValueSetter("Changed through R1"); MySingletonClass r2 = MySingletonClass.GetInstance(); Console.WriteLine("R2 value = {0}", r2.ValueGetter()); Console.ReadKey(); }
public void ShouldReturnSameInstanceWithMultipleCalls() { MySingletonClass first = MySingletonClass.Instance; MySingletonClass second = MySingletonClass.Instance; Assert.AreSame(first, second); first.SomeValue++; Assert.AreEqual(first.SomeValue, second.SomeValue); second.SomeValue++; Assert.AreEqual(first.SomeValue, second.SomeValue); first.Dispose(); second.Dispose(); }
public static MySingletonClass Instance() { if (_mySingletonObj == null) { lock (_mySingletonLock) { if (_mySingletonObj == null) { _mySingletonObj = new MySingletonClass(); } } } return(_mySingletonObj); }
protected override void Start() { System.Console.WriteLine(); MySingletonClass.DO(); System.Console.WriteLine(); Log.L("以上流程的执行顺序为:"); Log.L("1、调用静态方法Do"); Log.L("2、开始内部实例化出对象instance"); Log.L("3、调用构造函数"); Log.L("4、调用OnSingletonInit"); Log.L("5、DO方法调用完毕"); }
private void Merge(MySingletonClass otherInstance) { // do your merging here }