// Ajay kumar Dwivedi (adwiv@yahoo.com)
//
+//
+// Copyright (C) 2004 Novell, Inc (http://www.novell.com)
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+//
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+
using System.Runtime.Serialization;
using System.Runtime.InteropServices;
namespace System
{
- /// <summary>
- /// Summary description for WeakReference.
- /// </summary>
[Serializable]
+ [ComVisible (true)]
public class WeakReference : ISerializable
{
//Fields
// Helper method for constructors
//Should not be called from any other method.
- private void AllocateHandle(Object target)
+ private void AllocateHandle (Object target)
{
- if(this.isLongReference)
- {
- this.gcHandle = GCHandle.Alloc(target, GCHandleType.WeakTrackResurrection);
+ if (isLongReference) {
+ gcHandle = GCHandle.Alloc (target, GCHandleType.WeakTrackResurrection);
}
- else
- {
- this.gcHandle = GCHandle.Alloc(target, GCHandleType.Weak);
+ else {
+ gcHandle = GCHandle.Alloc (target, GCHandleType.Weak);
}
- }
-
-
+ }
+
//Constructors
- public WeakReference(object target)
- : this(target,false)
- {}
+#if NET_2_1
+ protected WeakReference ()
+ {
+ }
+#endif
+ public WeakReference (object target)
+ : this (target, false)
+ {
+ }
-
- public WeakReference(object target, bool trackResurrection)
+ public WeakReference (object target, bool trackResurrection)
{
- this.isLongReference = trackResurrection;
- AllocateHandle(target);
+ isLongReference = trackResurrection;
+ AllocateHandle (target);
}
-
- protected WeakReference(SerializationInfo info, StreamingContext context)
+ protected WeakReference (SerializationInfo info, StreamingContext context)
{
if (info == null)
throw new ArgumentNullException ("info");
- this.isLongReference = info.GetBoolean("IsLongReference");
- //TODO: How to load the exact type?
- //Does that matter? No idea :(
- Object target = info.GetValue("TargetObject",typeof(System.Object));
+ isLongReference = info.GetBoolean ("TrackResurrection");
+ Object target = info.GetValue ("TrackedObject", typeof (System.Object));
- AllocateHandle(target);
+ AllocateHandle (target);
}
-
// Properties
- public virtual bool IsAlive
- {
- get
- {
+ public virtual bool IsAlive {
+ get {
//Target property takes care of the exception
return (Target != null);
}
}
- public virtual object Target
- {
- get
- {
+ public virtual object Target {
+ get {
//Exception is thrown by gcHandle's Target
- return this.gcHandle.Target;
+ return gcHandle.Target;
}
set
{
- this.gcHandle.Target = value;
+ gcHandle.Target = value;
}
}
- public virtual bool TrackResurrection
- {
- get
- {
- return this.isLongReference;
+ public virtual bool TrackResurrection {
+ get {
+ return isLongReference;
}
}
//Methods
- ~WeakReference()
+ ~WeakReference ()
{
- gcHandle.Free();
+ gcHandle.Free ();
}
- //TODO
- public virtual void GetObjectData(SerializationInfo info,StreamingContext context)
+ public virtual void GetObjectData (SerializationInfo info, StreamingContext context)
{
if (info == null)
throw new ArgumentNullException ("info");
- info.AddValue("IsLongReference",this.isLongReference);
- try
- {
- info.AddValue("TargetObject",Target);
- }
- catch(Exception)
- {
- info.AddValue("TargetObject",null);
+ info.AddValue ("TrackResurrection", TrackResurrection);
+
+ try {
+ info.AddValue ("TrackedObject", Target);
+ } catch (Exception) {
+ info.AddValue ("TrackedObject", null);
}
}
}
-}
\ No newline at end of file
+}