2010-07-12 24 views
5

Tôi đang cố gắng sử dụng WeakReference loại an toàn trong ứng dụng Silverlight của mình. Tôi đang theo dõi công thức trên trang web này: http://ondevelopment.blogspot.com/2008/01/generic-weak-reference.html chỉ sử dụng System.WeakReference và bỏ qua nội dung tham chiếu Tuần tự hóa.Thừa kế WeakReference ném ReflectionTypeLoadException trong Silverlight

Nó ném một ReflectionTypeLoadException khi tôi cố gắng chạy nó, với thông điệp này:

"{System.TypeLoadException: quy tắc bảo mật Inheritance vi phạm trong khi trọng thành viên: 'Coatue.Silverlight.Shared.Cache.WeakReference`1. .ctor() '. Khả năng truy cập bảo mật của phương thức ghi đè phải phù hợp với khả năng truy cập bảo mật của phương pháp đang được ghi đè.} "

Bất kỳ đề xuất nào?

EDIT: Dưới đây là đoạn code tôi đang sử dụng:

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

thể bạn đăng mã cho lớp WeakReference của bạn? – jrista

+0

Được đăng ở trên (dưới dạng chỉnh sửa). – frank

Trả lời

5

Như Thomas đã đề cập, bạn không thể kế thừa từ tài liệu tham khảo yếu trong Silverlight nhưng bạn có thể quấn nó:

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

Giải pháp đơn giản và hiệu quả, +1. Tuy nhiên, tôi nghĩ bạn cũng cần một tài sản IsAlive;) –

+0

Bạn nói đúng, cảm ơn. –

+1

Lưu ý rằng bạn đang tạo hai đối tượng tham chiếu trên heap thay vì một ở đây. –

3

Có một nhu cầu thừa kế trên lớp WeakReference, và thời gian chạy Silverlight không có các quyền cần thiết. Vì vậy, bạn không thể kế thừa WeakReference trong 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; 
      } 
     } 
    } 
}