English 中文(简体)
Dictionary <K,V> that Implements IList <V>
原标题:Dictionary<K,V> that implements IList<V>

我经常面临这一问题: 我有一个字典,钥匙是一些简单的数字识别器,其价值是标的。 身份证还载于该价值物体的具体财产。

之后,我希望能够使一些(原样的)XML脱轨,例如:

<listitem>
    <id>20359</id>
    <someotherval>foo</someotherval>
</listitem>
<listitem>
    ...

这就要求我用<代码>List<V>进行脱硫,并且不必人工将这一条改为<代码>。 Dictionary<K,V>。

第二个问题是具有约束力的。 具有约束力的清单要求来文方执行<条码> 散列<>条码>(如果我记得正确的话),并且再次令来文提交人不得不人工制作一个新的<条码>;List<V>,并将其从<条码>中填入。 Dictionary<K,V>。

我目前的、公正但实用的解决办法是:

public abstract class Keyed<KeyType>
{
 public KeyType key { get; set; }
}

public class KeyedDictionary<KeyType, ValueType> :
 Dictionary<KeyType, ValueType>
 where ValueType : Keyed<KeyType>
{
 // ...
}

public class KeyedList<KeyType, ValueType> :
 IList<ValueType>,
 System.Collections.IList
 where ValueType : Keyed<KeyType>
{
 public readonly KeyedDictionary<KeyType, ValueType> dict =
  new KeyedDictionary<KeyType, ValueType>();

 // ...
}

这一工作在内部进行,但规模不大。 是否有更好的办法?


EDIT:这里是我即将解决的解决办法。

public interface IKeyed<KeyType>
{
    KeyType Key { get; }
}

public class KeyedList<KeyType, ValueType> :
    KeyedCollection<KeyType, ValueType>
    where ValueType : IKeyed<KeyType>
{
    protected override KeyType GetKeyForItem(ValueType item) { return item.Key; }
}
最佳回答

诸如以下设备:KeyedCollection<K,I> 类型可能会有trick。 它是一个抽象的课堂,因此,你需要打上自己的具体子级,但这要非常容易。

能够创建特殊项目,以达到贵国的需要,或者你可以创建一种总体项目,即: (每当有钥匙时,才会有特殊费用。)

var myKeyedByIdCollection =
    new ProjectionKeyedCollection<int, MyCustomType>(i => i.Id);

// ...

public class ProjectionKeyedCollection<TKey, TItem>
    : KeyedCollection<TKey, TItem>
{
    private readonly Func<TItem, TKey> _keySelector;

    public ProjectionKeyedCollection(Func<TItem, TKey> keySelector)
    {
        if (keySelector == null)
            throw new ArgumentNullException("keySelector");

        _keySelector = keySelector;
    }

    protected override TKey GetKeyForItem(TItem item)
    {
        return _keySelector(item);
    }
}
问题回答

究竟是哪一个是哪一个是哪一个是哪一个是哪一个是哪一个是哪一个? 这似乎不方便。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签