Can I create a List<WeakReference<T>>?

匆匆过客 提交于 2019-12-06 01:03:42

WeakReference is invariant, since it allows the value to be set, and that wouldn't be valid if it were covariant. To make it covariant you'll need to make a read only wrapper around the reference, and also use an interface.

public interface IReadOnlyWeakReference<out T>
{
    T Value { get; }
}

public class WeakReferenceWrapper<T> : IReadOnlyWeakReference<T>
    where T : class
{
    private WeakReference<T> reference;
    public WeakReferenceWrapper(WeakReference<T> reference)
    {
        this.reference = reference;
    }

    public T Value
    {
        get
        {
            T output;
            if (reference.TryGetTarget(out output))
                return output;
            else
                return default(T);
        }
    }
}

An extension method for the conversion is also somewhat convenient:

public static IReadOnlyWeakReference<T> AsReadOnly<T>(
    this WeakReference<T> reference)
    where T : class
{
    return new WeakReferenceWrapper<T>(reference);
}

Now we can write:

var mylist = new List<IReadOnlyWeakReference<Animal>>();

mylist.Add(new WeakReference<Animal>(new Animal()).AsReadOnly());
mylist.Add(new WeakReference<Tiger>(new Tiger()).AsReadOnly());
mylist.Add(new WeakReference<Wolf>(new Wolf()).AsReadOnly());

My suggestion is to create a list of WeakReferences (List) and expose it as an IEnumerable like this:

private List<WeakReference> innerList = new List<WeakReference>();

public IEnumerable<T> List
{
    get
    {
        return (this.innerList.Where(x => x.Target is T).Select(x => (T) x.Target));
    }
}
Sriram Sakthivel

You could simply use WeakReference<Animal> itself, anyways list is of type List<WeakReference<Animal>>, so you even with the covariance, you'll not be able to access the more derived members.

var mylist = new List<WeakReference<Animal>>();    
mylist.Add(new WeakReference<Animal>(new Animal()));
mylist.Add(new WeakReference<Animal>(new Tiger()));
mylist.Add(new WeakReference<Animal>(new Wolf()));

Or if you prefer to keep the more specific type, there is a way. I've solved it earlier with visitor pattern. See if that helps.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!