summaryrefslogtreecommitdiffstats
path: root/src/DotNetOpenAuth.Messaging/Messaging/KeyedCollectionDelegate.cs
diff options
context:
space:
mode:
authorAndrew Arnott <andrewarnott@gmail.com>2011-07-01 16:49:44 -0700
committerAndrew Arnott <andrewarnott@gmail.com>2011-07-01 16:49:44 -0700
commitb6f7a18b949acb4346754ae47fb07424076a3cd0 (patch)
tree4c23cb2b8174f3288cb0b787cff4c6ac432c6bef /src/DotNetOpenAuth.Messaging/Messaging/KeyedCollectionDelegate.cs
parentf16525005555b86151b7a1c741aa29550635108a (diff)
downloadDotNetOpenAuth-b6f7a18b949acb4346754ae47fb07424076a3cd0.zip
DotNetOpenAuth-b6f7a18b949acb4346754ae47fb07424076a3cd0.tar.gz
DotNetOpenAuth-b6f7a18b949acb4346754ae47fb07424076a3cd0.tar.bz2
First pass at dividing DotNetOpenAuth features into separate assemblies.
Nothing compiles at this point.
Diffstat (limited to 'src/DotNetOpenAuth.Messaging/Messaging/KeyedCollectionDelegate.cs')
-rw-r--r--src/DotNetOpenAuth.Messaging/Messaging/KeyedCollectionDelegate.cs45
1 files changed, 45 insertions, 0 deletions
diff --git a/src/DotNetOpenAuth.Messaging/Messaging/KeyedCollectionDelegate.cs b/src/DotNetOpenAuth.Messaging/Messaging/KeyedCollectionDelegate.cs
new file mode 100644
index 0000000..5555f9c
--- /dev/null
+++ b/src/DotNetOpenAuth.Messaging/Messaging/KeyedCollectionDelegate.cs
@@ -0,0 +1,45 @@
+//-----------------------------------------------------------------------
+// <copyright file="KeyedCollectionDelegate.cs" company="Andrew Arnott">
+// Copyright (c) Andrew Arnott. All rights reserved.
+// </copyright>
+//-----------------------------------------------------------------------
+
+namespace DotNetOpenAuth.Messaging {
+ using System;
+ using System.Collections.ObjectModel;
+ using System.Diagnostics.Contracts;
+
+ /// <summary>
+ /// A KeyedCollection whose item -&gt; key transform is provided via a delegate
+ /// to its constructor, and null items are disallowed.
+ /// </summary>
+ /// <typeparam name="TKey">The type of the key.</typeparam>
+ /// <typeparam name="TItem">The type of the item.</typeparam>
+ [Serializable]
+ internal class KeyedCollectionDelegate<TKey, TItem> : KeyedCollection<TKey, TItem> {
+ /// <summary>
+ /// The delegate that returns a key for the given item.
+ /// </summary>
+ private Func<TItem, TKey> getKeyForItemDelegate;
+
+ /// <summary>
+ /// Initializes a new instance of the KeyedCollectionDelegate class.
+ /// </summary>
+ /// <param name="getKeyForItemDelegate">The delegate that gets the key for a given item.</param>
+ internal KeyedCollectionDelegate(Func<TItem, TKey> getKeyForItemDelegate) {
+ Contract.Requires<ArgumentNullException>(getKeyForItemDelegate != null);
+
+ this.getKeyForItemDelegate = getKeyForItemDelegate;
+ }
+
+ /// <summary>
+ /// When implemented in a derived class, extracts the key from the specified element.
+ /// </summary>
+ /// <param name="item">The element from which to extract the key.</param>
+ /// <returns>The key for the specified element.</returns>
+ protected override TKey GetKeyForItem(TItem item) {
+ ErrorUtilities.VerifyArgumentNotNull(item, "item"); // null items not supported.
+ return this.getKeyForItemDelegate(item);
+ }
+ }
+}