using System;
using System.Collections.Generic;
using System.Reflection;
using Chernobyl.Attribution;
using Chernobyl.Reflection;
namespace Chernobyl.Extensions.System.Collections.Generic
{
///
/// Extensions for and related types.
///
public static class KeyValuePairExtensions
{
///
/// Returns a method that, when invoked, invokes the specified callback
/// with the value tied to a key.
///
/// The type of the key.
/// The type of the value.
/// The method to invoke with the key-value
/// pair.
/// The method that creates the key.
/// The method that converts the instance to a key-value pair.
public static Callback Key(
this Callback> callback,
Func createKey)
{
return callback.Select>(
value =>
{
var key = createKey(value);
return new KeyValuePair(key, value);
});
}
///
/// Returns a method that, when invoked, invokes the specified callback
/// with the value tied to a key retrieved from the
/// assigned to .
///
/// The type of the value.
/// The method to invoke with the key-value
/// pair.
/// The instance to pull the key from.
/// True to look up the hiearchy chain for the
/// attribute.
///
/// The method that converts the instance to a key-value pair.
///
public static Callback AttributeKey(
this Callback> callback,
ICustomAttributeProvider attributeProvider, bool inherit)
{
return callback.AttributeKey(attributeProvider, true, key => key);
}
///
/// Returns a method that, when invoked, invokes the specified callback
/// with the value tied to a key retrieved from the
/// assigned to .
///
/// The type of the key.
/// The type of the value.
/// The method to invoke with the key-value
/// pair.
/// The instance to pull the key from.
/// True to look up the hiearchy chain for the
/// attribute.
/// The method that converts the key.
///
/// The method that converts the instance to a key-value pair.
///
public static Callback AttributeKey(
this Callback> callback,
ICustomAttributeProvider attributeProvider, bool inherit,
Func keySelect)
{
return callback.Key(
subscribe =>
{
var key = attributeProvider.GetCustomAttribute(inherit).Key;
return keySelect(key);
});
}
}
}