我怎样才能得到一个KeyedCollection被只读? [英] How can I get a KeyedCollection to be read-only?

查看:179
本文介绍了我怎样才能得到一个KeyedCollection被只读?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先让我解释为什么我使用的是KeyedCollection。我建立一个DLL,我有我需要添加到集合,并让他们留在为了我放在他们,但我还需要通过双方的索引和键来访问这些项目的列表(关键是一个对象的属性,我已经定义)。如果有任何其他的简单集合,这样做,那么请让我知道。

First let me explain why I'm using a KeyedCollection. I'm building a DLL and I have a list of items that I need to add to a collection and have them stay in the order I placed them but I also need to access them by both their index and by key (the key is a property of an object which I already defined). If there is any other simpler collection that does this, then please let me know.

好了,现在,我需要能够项目在DLL内部加入到这个集合,但我需要为只读,因为我不想让他们删除/改变它是公开的,以DLL的最终用户我添加的项目。

Ok now, I need to be able to add items to this collection internally in the DLL but I need it to be publicly available to the DLL's end-user as read-only because I don't want them removing/altering the items I added.

我已经找遍了这个网站,其他网站,谷歌在一般的,我一直没能找到一种方式来获得某种只读KeyedCollection。我来最接近的是此页面(<一href="http://www.koders.com/csharp/fid27249B31BFB645825BD9E0AFEA6A2CCDDAF5A382.aspx?s=keyedcollection#L28" rel="nofollow">http://www.koders.com/csharp/fid27249B31BFB645825BD9E0AFEA6A2CCDDAF5A382.aspx?s=keyedcollection#L28)但我不能完全得到它的工作。

I've searched all over this site, other sites, google in general and I have not been able to find a way to get some sort of read-only KeyedCollection. The closest I came was this page (http://www.koders.com/csharp/fid27249B31BFB645825BD9E0AFEA6A2CCDDAF5A382.aspx?s=keyedcollection#L28) but I couldn't quite get it to work.

更新:

我看了看那些C5类。也就是说,随着你的其他意见,帮助我更好地了解如何创建自己的只读类,它似乎工作。不过,我有一个问题,当我试着投了定期一到只读的。我得到一个编译时不能转换错误。下面是我创建的code(第一小类是我本来有):

I took a look at those C5 classes. That, along with your other comments, helped me better understand how to create my own read-only class and it seems to work. However I have a problem when I try to cast the regular one to the read-only one. I get a compile-time cannot convert error. Here's the code I created (the first small class is what I originally had):

public class FieldCollection : KeyedCollection<string, Field>
{
    protected override string GetKeyForItem(Field field)
    {
        return field.Name;
    }
}

public class ReadOnlyFieldCollection : KeyedCollection<string, Field>
{
    protected override string GetKeyForItem(Field field)
    { return field.Name; }

    new public void Add(Field field)
    { throw new ReadOnlyCollectionException("This collection is read-only."); }

    new public void Clear()
    { throw new ReadOnlyCollectionException("This collection is read-only."); }

    new public void Insert(int index, Field field)
    { throw new ReadOnlyCollectionException("This collection is read-only."); }

    new public bool Remove(string key)
    { throw new ReadOnlyCollectionException("This collection is read-only."); }

    new public bool Remove(Field field)
    { throw new ReadOnlyCollectionException("This collection is read-only."); }

    new public bool RemoveAt(int index)
    { throw new ReadOnlyCollectionException("This collection is read-only."); }
}

如果我定义这个变量:

private FieldCollection _fields;

那么这样做:

then do this:

public ReadOnlyFieldCollection Fields;
Fields = (ReadOnlyFieldCollection)_fields;

它无法编译。他们都来自同一个类继承,我认为他们将是兼容。我怎么能投(或暴露)的集合为只读型我刚刚创建的?

it fails to compile. They're both inheriting from the same class, I thought they would be "compatible". How can I cast (or expose) the collection as the read-only type I just created?

推荐答案

我不知道任何内置的解决方案,以及。这是我给的意见建议的一个例子:

I don't know of any built-in solution as well. This is an example of the suggestion I gave on the comment:

    public class LockedDictionary : Dictionary<string, string>
    {
        public override void Add(string key, string value)
        {
            //do nothing or log it somewhere
            //or simply change it to private
        }

        //and so on to Add* and Remove*

        public override string this[string i]
        {
            get
            {
                return base[i];
            }
            private set
            {
                //...
            }
        }
    }

您就可以遍历KeyValuePair列表和其他一切,但它不能用于写入操作。 只是一定要根据您的需要输入。

You'll be able to iterate through the KeyValuePair list and everything else, but it won't be available for writing operations. Just be sure to type it according to your needs.

修改 T,T&GT;为了有一个排序的列表,我们可以从词典&LT改变基类的Hashtable

EDIT In order to have a sorted list, we can change the base class from Dictionary<T,T> to a Hashtable.

这是这样的:

    public class LockedKeyCollection : System.Collections.Hashtable
    {
        public override void Add(object key, object value)
        {
            //do nothing or throw exception?
        }

        //and so on to Add* and Remove*

        public override object this[object i]
        {
            get
            {
                return base[i];
            }
            set
            {
                //do nothing or throw exception?
            }
        }
    }

用法:

        LockedKeyCollection aLockedList = new LockedKeyCollection();

        foreach (System.Collections.DictionaryEntry entry in aLockedList)
        {
            //entry.Key
            //entry.Value
        }

不幸的是,我们无法改变的方法的访问修饰符,但我们可以覆盖他们什么都不做。

Unfortunately, we can't change the access modifier of the methods, but we can override them to do nothing.

这篇关于我怎样才能得到一个KeyedCollection被只读?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆