#region Copyright 2011-2014 by Roger Knapp, Licensed under the Apache License, Version 2.0
/* Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#endregion
using System;
namespace CSharpTest.Net.Utils
{
///
/// A strong-typed derivation of the WeakReference class
///
[Serializable]
public class WeakReference : WeakReference
where T : class
{
/// Creates a new WeakReference that keeps track of target.
public WeakReference(T instance)
: base(instance)
{ }
///
protected WeakReference(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context)
: base(info, context)
{ }
///
/// Gets an indication whether the object referenced by the current object has been garbage collected.
///
public override bool IsAlive { get { return base.IsAlive && base.Target is T; } }
/// Gets or sets the Object stored in the handle if it's accessible.
public new T Target
{
get { return base.Target as T; }
set { base.Target = value; }
}
/// Returns true if the Object was retrieved.
public bool TryGetTarget(out T value)
{
value = base.Target as T;
return value != null;
}
}
}