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 に答える