Inherited WeakReference throwing ReflectionTypeLoadException in Silverlight
I'm trying to use a type-safe WeakReference in my Silverlight app. I'm following the recipe on this site: http://ondevelopment.blogspot.com/2008/01/generic-weak-reference.html only using the System.WeakReference and omitting the stuff that references Serialization.
It's throwing a ReflectionTypeLoadException when I try to run it, with this message:
"{System.TypeLoadException: Inheritance security rules violated while overriding member: 'Coatue.Silverlight.Shared.Cache.WeakReference`1..ctor()'. Security accessibility of the overriding method must match the security accessibility of the method being overriden.}"
Here's the code I'm using:
using System;
namespace Frank
{
public class WeakReference<T>
: WeakReference where T : class
{
public WeakReference(T target)
: base(target) { }
public WeakReference(T target, bool trackResurrection)
: base(target, t开发者_开发问答rackResurrection) { }
protected WeakReference() : base() { }
public new T Target
{
get
{
return (T)base.Target;
}
set
{
base.Target = value;
}
}
}
}
As Thomas mentioned, you can't inherit from weak reference in silverlight but you can wrap it:
using System;
namespace Frank
{
public class WeakReference<T> where T : class
{
private readonly WeakReference inner;
public WeakReference(T target)
: this(target, false)
{ }
public WeakReference(T target, bool trackResurrection)
{
if(target == null) throw new ArgumentNullException("target");
this.inner = new WeakReference(target, trackResurrection);
}
public T Target
{
get
{
return (T)this.inner.Target;
}
set
{
this.inner.Target = value;
}
}
public bool IsAlive {
get {
return this.inner.IsAlive;
}
}
}
}
There is an inheritance demand on the WeakReference
class, and the Silverlight runtime doesn't have the necessary permissions. So you can't inherit WeakReference
in Silverlight...
[SecurityPermissionAttribute(SecurityAction.InheritanceDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
using System;
namespace Harmony.Ria
{
public class WeakReference<T>
where T : class
{
private WeakReference inner;
/// <summary>
/// Initializes a new instance of the System.WeakReference class, referencing
/// the specified object.
/// </summary>
/// <param name="target">The object to track or null.</param>
public WeakReference(T target)
: this(target, false)
{ }
/// <summary>
/// Initializes a new instance of the System.WeakReference class, referencing
/// the specified object and using the specified resurrection tracking.
/// </summary>
/// <param name="target">An object to track.</param>
/// <param name="trackResurrection">Indicates when to stop tracking the object.
/// If true, the object is tracked after finalization; if false, the object is
/// only tracked until finalization.</param>
public WeakReference(T target, bool trackResurrection)
{
if (target == null) throw new ArgumentNullException("target");
this.inner = new WeakReference((object)target, trackResurrection);
}
/// <summary>
/// Gets or sets the object (the target) referenced by the current
/// System.WeakReference object.
/// </summary>
public T Target { get { return (T)this.inner.Target; } set { this.inner.Target = value; } }
/// <summary>
/// Gets an indication whether the object referenced by the current
/// System.WeakReference object has been garbage collected.
/// </summary>
public bool IsAlive { get { return this.inner.IsAlive; } }
/// <summary>
/// Casts an object of the type T to a weak reference
/// of T.
/// </summary>
public static implicit operator WeakReference<T>(T target)
{
if (target == null)
{
throw new ArgumentNullException("target");
}
return new WeakReference<T>(target);
}
/// <summary>
/// Casts a weak reference to an object of the type the
/// reference represents.
/// </summary>
public static implicit operator T(WeakReference<T> reference)
{
if (reference != null)
{
return reference.Target;
}
else
{
return null;
}
}
}
}
精彩评论