2010-07-12 10 views
5

Estoy tratando de utilizar WeakReference seguro de tipo en mi aplicación Silverlight. Estoy siguiendo la receta en este sitio: http://ondevelopment.blogspot.com/2008/01/generic-weak-reference.html solo usando System.WeakReference y omitiendo las cosas que hacen referencia a la serialización.WeedReference heredado arrojando ReflectionTypeLoadException en Silverlight

Es lanzando una ReflectionTypeLoadException cuando intento ejecutarlo, con este mensaje:

"{System.TypeLoadException: normas de seguridad de herencia violado mientras que el miembro anular: 'Coatue.Silverlight.Shared.Cache.WeakReference`1. .ctor() '. La accesibilidad de seguridad del método de anulación debe coincidir con la accesibilidad de seguridad del método que se reemplaza.} "

¿Alguna sugerencia?

EDIT: Aquí está el código que estoy usando:

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, trackResurrection) { } 

     protected WeakReference() : base() { } 

     public new T Target 
     { 
      get 
      { 
       return (T)base.Target; 
      } 
      set 
      { 
       base.Target = value; 
      } 
     } 
    } 
} 
+0

Podría publicar el código para su clase WeakReference? – jrista

+0

Publicado más arriba (como edición). – frank

Respuesta

5

Como Thomas mencionó, no se puede heredar de referencia débil en Silverlight, pero se puede envolver:

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; 
      } 
     } 
    } 
} 
+0

Solución simple y eficiente, +1. Sin embargo, creo que también necesita una propiedad IsAlive;) –

+0

Tiene razón, gracias. –

+1

Tenga en cuenta que está creando dos objetos de referencia en el montón en lugar de uno aquí. –

3

Hay una petición de herencia de la clase WeakReference, y el tiempo de ejecución de Silverlight no tiene los permisos necesarios. Así no se puede heredar WeakReference en Silverlight ...

[SecurityPermissionAttribute(SecurityAction.InheritanceDemand, Flags = SecurityPermissionFlag.UnmanagedCode)] 
0
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; 
      } 
     } 
    } 
} 
Cuestiones relacionadas