5

我正在尝试在我的 Silverlight 应用程序中使用类型安全的 WeakReference。我正在遵循此站点上的食谱:http: //ondevelopment.blogspot.com/2008/01/generic-weak-reference.html仅使用 System.WeakReference 并省略引用序列化的内容。

当我尝试运行它时,它会抛出一个 ReflectionTypeLoadException,并带有以下消息:

“{System.TypeLoadException:覆盖成员时违反了继承安全规则:'Coatue.Silverlight.Shared.Cache.WeakReference`1..ctor()'。覆盖方法的安全可访问性必须与被覆盖方法的安全可访问性相匹配。 }"

有什么建议么?

编辑:这是我正在使用的代码:

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;
            }
        }
    }
}
4

3 回答 3

5

正如 Thomas 所提到的,您不能从 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;
            }
        }
    }
}
于 2010-07-12T21:38:38.410 回答
3

类有继承需求WeakReference,Silverlight 运行时没有必要的权限。所以你不能WeakReference在 Silverlight 中继承......

[SecurityPermissionAttribute(SecurityAction.InheritanceDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
于 2010-07-12T21:31:54.020 回答
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;
            }
        }
    }
}
于 2011-02-21T11:54:11.270 回答