//****************************************************************************************************** // NullableWeakReference.cs - Gbtc // // Copyright © 2017, Grid Protection Alliance. All Rights Reserved. // // Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See // the NOTICE file distributed with this work for additional information regarding copyright ownership. // The GPA licenses this file to you under the MIT License (MIT), the "License"; you may // not use this file except in compliance with the License. You may obtain a copy of the License at: // // http://www.opensource.org/licenses/MIT // // Unless agreed to in writing, the subject software distributed under the License is distributed on an // "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the // License for the specific language governing permissions and limitations. // // Code Modification History: // ---------------------------------------------------------------------------------------------------- // 02/14/2017 - Steven E. Chisholm // Generated original version of source code. // //****************************************************************************************************** using System; namespace GSF { /// /// A implementation that can have the object set to null. /// Natively, setting to null will throw an , /// [Serializable] public class NullableWeakReference : WeakReference { private bool m_cleared; /// /// Creates a /// /// the object to maintain the weak reference to. Cannot be null. public NullableWeakReference(object target) : base(target) { if (target == null) throw new ArgumentNullException(nameof(target)); } /// /// Gets an indication whether the object referenced by the current object has been cleared or garbage collected. /// /// /// true if the object referenced by the current object has not been garbage collected or cleared /// and is still accessible; otherwise, false. /// /// 2 public override bool IsAlive => !m_cleared && base.IsAlive; /// /// Gets the object (the target) referenced by the current object. /// Set will only accept null. /// /// /// null if the object referenced by the current object has been garbage collected or cleared; /// otherwise, a reference to the object referenced by the current object. /// /// If setting this property to anything other than null /// 2 public override object Target { get { if (m_cleared) return null; return base.Target; } set { if (value == null) { Clear(); } else { throw new InvalidOperationException("This target must be set in the constructor."); } } } /// /// Sets to null so subsequent calls to returns null /// public virtual void Clear() { m_cleared = true; } } }