2010-07-12 4 views
5

J'essaie d'utiliser un WeakReference de type sécurité dans mon application Silverlight. Je suis la recette sur ce site: http://ondevelopment.blogspot.com/2008/01/generic-weak-reference.html seulement en utilisant le System.WeakReference et en omettant le truc qui fait référence à la sérialisation.Inherited WeakReference lançant ReflectionTypeLoadException dans Silverlight

C'est de lancer une ReflectionTypeLoadException lorsque je tente de l'exécuter, avec ce message:

« {System.TypeLoadException: règles de sécurité de succession tout en remplaçant membre enfreint: « Coatue.Silverlight.Shared.Cache.WeakReference`1. .ctor() '. L'accessibilité de la méthode prioritaire pour la sécurité doit correspondre à celle de la méthode en cours de modification.} "

Des suggestions?

EDIT: Voici le code que je utilise:

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

Pourriez-vous poster le code pour votre classe WeakReference? – jrista

+0

Publié ci-dessus (comme éditer). – frank

Répondre

5

Comme Thomas mentionné, vous ne pouvez pas hériter de référence faible en silverlight mais vous pouvez envelopper:

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

Solution de contournement simple et efficace, +1. Cependant, je pense que vous avez également besoin d'une propriété IsAlive;) –

+0

Vous avez raison, merci. –

+1

Notez que vous créez deux objets de référence sur le tas au lieu d'un ici. –

3

Il y a une demande d'héritage de la classe WeakReference, et le moteur d'exécution Silverlight ne dispose pas des autorisations nécessaires. Donc, vous ne pouvez pas hériter WeakReference dans 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; 
      } 
     } 
    } 
} 
Questions connexes