Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#:如何在不公开字典的情况下浏览字典项?_C#_Dictionary - Fatal编程技术网

C#:如何在不公开字典的情况下浏览字典项?

C#:如何在不公开字典的情况下浏览字典项?,c#,dictionary,C#,Dictionary,该类如下所示: public static class CacheManager { private static Dictionary<string, object> cacheItems = new Dictionary<string, object>(); private static ReaderWriterLockSlim locker = new ReaderWriterLockSlim(); public static Dicti

该类如下所示:

public static class CacheManager
{
    private static Dictionary<string, object> cacheItems = new Dictionary<string, object>();

    private static ReaderWriterLockSlim locker = new ReaderWriterLockSlim();

    public static Dictionary<string, object> CacheItems
    {
        get
        {
            return cacheItems;
        }
    }
    ...
}
公共静态类缓存管理器
{
private static Dictionary cacheItems=new Dictionary();
私有静态ReaderWriterLockSlim locker=新的ReaderWriterLockSlim();
公共静态字典缓存项
{
得到
{
归还物品;
}
}
...
}
还应使用ReaderWriterLockSlim locker对象

客户端现在看起来如下所示:

foreach (KeyValuePair<string, object> dictItem in CacheManager.CacheItems)
{
    ...
}
foreach(CacheManager.CacheItems中的KeyValuePair术语)
{
...
}

提前感谢。

是否在筛选的词典上公开只读视图属性?尽可能公开允许访问项的方法


到底是什么问题?“不将字典公开为公共”是不够含糊的,这是不可回答的。

这个属性如何:

public static IEnumerable<KeyValuePair<string, object> CacheItems
{
    get
    {
        return cacheItems;
    }
}

publicstaticienumerable如果您只需要迭代内容,那么坦率地说,它实际上并没有被用作字典,但是可以使用迭代器块和索引器来隐藏内部对象:

public IEnumerable<KeyValuePair<string, object>> CacheItems
{
    get
    { // we are not exposing the raw dictionary now
        foreach(var item in cacheItems) yield return item;
    }
}
public object this[string key] { get { return cacheItems[key]; } }
public IEnumerable缓存项
{
得到
{//我们现在不公开原始词典
foreach(cacheItems中的var项)产生返回项;
}
}
公共对象此[string key]{get{return cacheItems[key];}

取决于您可以使用的词典的大小



您可能还想查看该属性。

Java?您可以公开字典或键的只读视图。或者看看访客模式,它是c#。字典的只读视图是什么?我需要KeyValuePair字典项。您为什么不使用并发字典?出现了什么错误?你想达到什么目的?这仍然可以追溯到最初的字典。我已经考虑过了。“这是否意味着这本词典不会公开发表?”马克·格雷威尔,我同意。我认为这和我在原来的帖子中所说的一样,问题是它实际上返回了dictionary对象,只是伪装成了其他的东西。你可以很容易地把它放回字典,然后你基本上就拥有了原来的字典。马克:这通常被认为是一种不好的做法吗?我通常将“私有”列表返回为iQueDaby,并将其视为只读。您是说应该始终复制返回的集合吗?“不将字典公开为公共”意味着用其他内容替换CacheItems属性。@tesicg所以您希望字典(及其内容)为只读?@trispid,是的。我认为我在我的原始帖子中使用了get子句。get使字典成为只读的,而不是它的内容。我同意ToToM,您可以考虑将迭代器方法放入类中,只公开字典搜索结果(如果存在)。在20k销售代表处,我本以为你知道得更清楚。:)类需要是静态的。看来我不用索引器也能用这个?对吗?@tesicg:那又怎样?让它静止。你想学习怎么做,还是想让其他人为你编程?@reinierpost,不。正如你所看到的,我的代码部分已经在上面了。但是,我只是说我不需要索引器。仅此而已。@tesicg如果您不需要通过索引进行访问,那么我会问为什么它是一本词典,但是。。。只要符合你的要求requirements@rawling谢谢你的修复。。。离开我的办公桌前匆忙地打字
private static ReadOnlyDictionary<string, object> _CacheReadOnly;
private static Dictionary<string, object> _CacheItems;

public static ctor()
{
    _CacheItems = new Dictionary<string, object>();
    _CacheReadOnly = new ReadOnlyDictionary(_CacheItems);
}

public static IDictionary<string, object> CacheItems
{
    get
    {
        return CacheReadOnly;
    }
}
public static IEnumerable<KeyValuePair<string, object> CacheItems
{
    get
    {
        return cacheItems.Select(x => x);
    }
}
public IEnumerable<KeyValuePair<string, object>> CacheItems
{
    get
    { // we are not exposing the raw dictionary now
        foreach(var item in cacheItems) yield return item;
    }
}
public object this[string key] { get { return cacheItems[key]; } }