Silverlight 中继承的 WeakReference 抛出 ReflectionTypeLoadException

发布于 2024-09-09 07:17:08 字数 1133 浏览 1 评论 0原文

我正在尝试在我的 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;
            }
        }
    }
}

I'm trying to use a type-safe WeakReference in my Silverlight app. I'm following the recipe on this site: http://ondevelopment.blogspot.com/2008/01/generic-weak-reference.html only using the System.WeakReference and omitting the stuff that references Serialization.

It's throwing a ReflectionTypeLoadException when I try to run it, with this message:

"{System.TypeLoadException: Inheritance security rules violated while
overriding member:
'Coatue.Silverlight.Shared.Cache.WeakReference`1..ctor()'. Security
accessibility of the overriding method must match the security
accessibility of the method being overriden.}"

Here's the code I'm using:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

寒尘 2024-09-16 07:17:08

正如 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;
            }
        }
    }
}

As Thomas mentioned, you can't inherit from weak reference in silverlight but you can wrap it:

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;
            }
        }
    }
}
年少掌心 2024-09-16 07:17:08

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

[SecurityPermissionAttribute(SecurityAction.InheritanceDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]

There is an inheritance demand on the WeakReference class, and the Silverlight runtime doesn't have the necessary permissions. So you can't inherit WeakReference in Silverlight...

[SecurityPermissionAttribute(SecurityAction.InheritanceDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
暮光沉寂 2024-09-16 07:17:08
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;
            }
        }
    }
}
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;
            }
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文