[Mono-list] System.WeakReference
Dwivedi , Ajay Kumar
AjayKumar.Dwivedi@dresdner-bank.com
Sun, 24 Feb 2002 11:13:17 -0000
This message is in MIME format. Since your mail reader does not understand
this format, some or all of this message may not be legible.
------_=_NextPart_000_01C1BD24.424B2DA0
Content-Type: text/plain;
charset="iso-8859-1"
hi all,
The code for System.WeakReference is attached. Can someone add it to
the CVS. It needs GCHandle class, so will not compile yet on mono.
But there are a few queries:
1. The whole WeakReference is based on GCHandleType enum in
System.Runtime.InteropServices. The Mono implementation of this class has
only two members Normal, and Pinned. I can add the other two, But where can
we find the internal values for other two members. IMO it would be necessary
to have the same values for the enum members as MS's implementation. But I
am not sure.
2. The same thing goes for Serialization. We give a string tag while
adding an object to SerializationInfo. Should we have the same tags as the
MS's implementation.
3. I am not sure about Resurrection thing.
Happy Hacking,
Ajay kumar Dwivedi
--
#!!! If anything can go wrong, _FIX_ it. (To hell with MURPHY)
Dwivedi, Ajay kumar
<<WeakReference.cs>>
------_=_NextPart_000_01C1BD24.424B2DA0
Content-Type: application/octet-stream;
name="WeakReference.cs"
Content-Disposition: attachment;
filename="WeakReference.cs"
//
// System.WeakReference.cs
//
// Author:
// Ajay kumar Dwivedi (adwiv@yahoo.com)
//
using System.Runtime.Serialization;
using System.Runtime.InteropServices;
namespace System
{
/// <summary>
/// Summary description for WeakReference.
/// </summary>
[Serializable]
public class WeakReference : ISerializable
{
//Fields
private bool isLongReference;
private GCHandle gcHandle;
// Helper method for constructors
//Should not be called from any other method.
private void AllocateHandle(Object target)
{
if(this.isLongReference)
{
this.gcHandle = GCHandle.Alloc(target, GCHandleType.WeakTrackResurrection);
}
else
{
this.gcHandle = GCHandle.Alloc(target, GCHandleType.Weak);
}
}
//Constructors
public WeakReference(object target)
: this(target,false)
{}
public WeakReference(object target, bool trackResurrection)
{
this.isLongReference = trackResurrection;
AllocateHandle(target);
}
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));
AllocateHandle(target);
}
// Properties
public virtual bool IsAlive
{
get
{
//Target property takes care of the exception
return (Target != null);
}
}
public virtual object Target
{
get
{
//Exception is thrown by gcHandle's Target
return this.gcHandle.Target;
}
set
{
this.gcHandle.Target = value;
}
}
public virtual bool TrackResurrection
{
get
{
return this.isLongReference;
}
}
//Methods
~WeakReference()
{
gcHandle.Free();
}
//TODO
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);
}
}
}
}
------_=_NextPart_000_01C1BD24.424B2DA0--