繁体   English   中英

返回HashSet <T> 来自泛型函数中泛型类型的HashSet

[英]Return HashSet<T> from HashSet of generic type in generic function

我有一个Dictionary<Type, HashSet<GenericType>> ,可用于保存数据,并且我试图制作一个返回给定泛型T : GenericType HashSet之一的函数。

基本上

Dictionary<Type, HashSet<GenericType>> data;

public HashSet<T> Get<T>() where T : GenericType
{
    var tp = typeof(T);
    //....check if its in the dictionary, fill if not....
    return data[tp];
}

当然这是无效的。 但是我很难弄清楚应该怎么做。 我觉得返回T是最佳选择,因为您可以执行以下操作:

Get<Derived>().Where(x => x.DerivedProperty == someValue)

但是我唯一想到的是每次调用Get都创建一个新的HashSet<T> ,然后使用foreach循环强制转换并从Dictionary中已经存在的HashSet中添加所有项,但这感觉像是浪费?

另一个想法是跳过HashSet并使用另一个(协变量?)集合。 但是,由于这些集合将保存大量数据,因此也不是最好的主意。

简而言之,我想知道解决此问题的最佳方法是什么。

更新

这就是我得到的结构。 在我的代码结构中,包含data的类型是一种服务类型。 它会通过反射在运行时加载和初始化。 之后,我从那里使用各种ServiceFactory来获取该服务。

public class foo : Service
{
    public Dictionary<Type, HashSet<BaseClass>> data = new Dictionary<Type, HashSet<BaseClass>>();

    public T Get<T>() where T : BaseClass
    {
        var tp = typeof(T);

        if (!data.ContainsKey(tp))
        {
            data.Add(typeof(Derived), new HashSet<BaseClass>() { new Derived(), new Derived(), new Derived() });
        }

        return data[tp];//this wont compile.
    }
}

public class Derived : BaseClass
{
    public int ExampleVariable {get;set;}
}

public abstract class BaseClass
{
    // some things in here.
    public void DoCommonStuff()
    {

    }
}

class program
{

    static void Main(string[] args)
    {
        var service = ServiceFactory.GetService<foo>();
        var collection = service.Get<Derived>();
    }
}

我只需要更改字典的类型,然后将其强制转换为Get方法即可。 当然,绝对将您的词典设为私有-然后,您可以确保只有您的代码 (最好是只有Get方法)才能访问它:

// Any data[typeof(Foo)] value will be a HashSet<Foo>. Only
// the Get method should access this dictionary.
private readonly Dictionary<Type, object> data = new Dictionary<Type, object>();

public HashSet<T> Get<T>() where T : GenericType
{
    var tp = typeof(T);
    object value;
    if (data.TryGetValue(tp, out value))
    {
        return (HashSet<T>) value;
    }

    var newSet = new HashSet<T>()
    // Populate newSet here
    data[tp] = newSet;
    return newSet;
}

我在博客文章中对一个密切相关的问题进行了更多讨论。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM