【发布时间】:2014-06-14 07:10:58
【问题描述】:
我必须编写一个方法,将字符串、int、short、long、float 或 double 作为参数接收,然后为该参数分配一个随机值并将其存储在静态 ConcurrentDictionary 中。性能是一个主要限制因素,所以我不想采用会牺牲性能的设计
例子:
public void StoreVal<T>(T val)
{
//Check if the val is already in the respective dictionary
//If not, then create a random value
//Store both values in the dictionary
}
我为我期望的每个数据类型创建了一个静态 ConcurrentDictionary。我现在面临的关键问题是如何从泛型方法中引用正确的集合类型,而不必使用一大堆 if/else 语句?
更新:我正在使用 ConcurrentDictionary,因为此方法将由 8 个线程(至少)调用,并且我必须确保传递的参数只有一个映射值。另一个约束是每个数据类型都应该有它自己的映射,即如果 10 (int) -> 25 (int),那么 10 (short) 不需要指向 25 (short) - 这就是为什么我创建了一个单独的 ConcurrentDictionary每种数据类型。
【问题讨论】:
-
这是一项任务,还是您可以灵活地实施?
-
@hatchet 我已根据您的回复更新了我的问题。
标签: c# generics design-patterns concurrency