2010-07-12 23 views
5

我想在我的Silverlight應用程序中使用類型安全的WeakReference。我遵循這個網站上的配方:http://ondevelopment.blogspot.com/2008/01/generic-weak-reference.html只使用System.WeakReference並省略引用序列化的內容。在Silverlight中拋出ReflectionTypeLoadException繼承的WeakReference

它拋出一個ReflectionTypeLoadException當我嘗試運行它,此消息:

「{System.TypeLoadException:「Coatue.Silverlight.Shared.Cache.WeakReference`1:繼承安全,同時覆蓋成員違反了規則。 。「()'。覆蓋方法的安全性可訪問性必須與被覆蓋方法的安全性可訪問性相匹配}}

有什麼建議嗎?

編輯:下面是我使用的代碼:

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

你能發佈您的WeakReference類的代碼? – jrista 2010-07-12 20:23:18

+0

上面發佈(如編輯)。 – frank 2010-07-12 20:35:33

回答

5

正如托馬斯所說,你不能在Silverlight弱引用繼承,但是你可以用它:

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

簡單而有效的解決方法,+1。不過,我認爲你還需要一個IsAlive屬性;) – 2010-07-12 23:26:41

+0

你是對的,謝謝。 – 2010-07-13 13:30:58

+1

請注意,您正在堆中創建兩個引用對象,而不是一個。 – 2011-02-12 15:34:01

3

上有WeakReference類繼承需求,Silverlight運行時沒有必要的權限。所以,你不能繼承在Silverlight WeakReference ...

[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; 
      } 
     } 
    } 
} 
相關問題