加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 在这个.NET代码中遇到Generics的问题

发布时间:2020-12-16 01:38:08 所属栏目:百科 来源:网络整理
导读:我正在尝试制作混合类型的集合.我知道一开始的类型..但我似乎无法弄清楚制作集合的语法等. 例如. ....// I leave the typo there,for embarrassment :(Initialize(new []{ typeof(Cat),typeof(Dog),typeof(JohnSkeet) }); ...public Foo Initialize(IEnumera
我正在尝试制作混合类型的集合.我知道一开始的类型..但我似乎无法弄清楚制作集合的语法等.

例如.

....
// I leave the typo there,for embarrassment :(
Initialize(new []{ typeof(Cat),typeof(Dog),typeof(JohnSkeet) }); 
...

public Foo Initialize(IEnumerable<Type> types)
{
   // for each type,set up the inmemory storage.
   foreach(var type in types)
   {
       // ????
       // Create an empty list,which will only contain this 'type'
       // I'm guessing,an IDictionary<type,ICollection<type>>().. thingy ?
   }
}

public ICollection<Type> SomeTypeData(Type type)
{
    // Return the collection,for this type.
}

这鬃毛有意义吗?这可能吗?

解决方法

在我看来,你正在尝试创建某种实例存储库;存储给定类型的实例列表的类.

这是一个示例实现.我已经包含了SomeTypeData方法的泛型和非泛型版本:

public class InstanceRepository
{
    private IDictionary<Type,ICollection> _Instances = new Dictionary<Type,ICollection>();

    public ICollection SomeTypeData(Type type)
    {
        ICollection instanceList;
        if (!_Instances.TryGetValue(type,out instanceList))
        {
            // this type does not exist in our dictionary,so let's create a new empty list

            // we could do this:
            //instanceList = new List<object>();

            // but let's use reflection to make a more type-specific List<T> instance:
            instanceList = (ICollection)Activator.CreateInstance(typeof(List<>).MakeGenericType(type));

            // now add it to the dictionary
            _Instances.Add(type,instanceList);
        }
        // Return the collection,for this type.
        return instanceList;
    }

    public IList<T> SomeTypeData<T>()
    {
        Type type = typeof(T);
        ICollection instanceList;
        if (!_Instances.TryGetValue(typeof(T),out instanceList))
        {
            instanceList = new List<T>();
            _Instances.Add(type,instanceList);
        }
        // here we are assuming that all of the lists in our dictionary implement IList<T>.
        // This is a pretty safe assumption,since the dictionary is private and we know that
        // this class always creates List<T> objects to put into the dictionary.
        return (IList<T>)instanceList;
    }
}

以下是一个用法示例:

通用:

InstanceRepository repository = new InstanceRepository();

        var listOfCats = repository.SomeTypeData<Cat>();

        listOfCats.Add(new Cat());

        Cat firstCat = listOfCats[0];

        Console.WriteLine(listOfCats.GetType().FullName);

非通用:

InstanceRepository repository = new InstanceRepository();

        var listOfCats = (IList<Cat>)repository.SomeTypeData(typeof(Cat));

        listOfCats.Add(new Cat());

        Cat firstCat = listOfCats[0];

        Console.WriteLine(listOfCats.GetType().FullName);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读