继承 WeakReference 在 Silverlight 中抛出 ReflectionTypeLoadException [英] Inherited WeakReference throwing ReflectionTypeLoadException in Silverlight

查看:14
本文介绍了继承 WeakReference 在 Silverlight 中抛出 ReflectionTypeLoadException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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

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

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

推荐答案

正如 Thomas 提到的,你不能从 Silverlight 中的弱引用继承,但你可以包装它:

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

这篇关于继承 WeakReference 在 Silverlight 中抛出 ReflectionTypeLoadException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆