【问题标题】:Inherited WeakReference throwing ReflectionTypeLoadException in Silverlight继承的 WeakReference 在 Silverlight 中抛出 ReflectionTypeLoadException
【发布时间】:2010-07-12 20:21:36
【问题描述】:

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

【问题讨论】:

  • 你能发布你的 WeakReference 类的代码吗?

标签: c# silverlight weak-references


【解决方案1】:

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

【讨论】:

  • 简单高效的解决方法,+1。但是,我认为您还需要一个 IsAlive 属性;)
  • 请注意,您在堆上创建了两个引用对象,而不是在这里创建一个。
【解决方案2】:

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

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

【讨论】:

    【解决方案3】:
    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-08-01
      • 1970-01-01
      • 2011-11-14
      • 1970-01-01
      • 2011-08-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多