140 lines
5 KiB
C#
140 lines
5 KiB
C#
using System;
|
|
|
|
namespace JD.Kernel.StaticHelpers
|
|
{
|
|
/// <summary>
|
|
/// A basic converter that takes a <typeparamref name="T"/> and returns a new <typeparamref name="T"/>.
|
|
/// </summary>
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <param name="input"></param>
|
|
/// <returns></returns>
|
|
public delegate T Converter<T>(T input);
|
|
|
|
/// <summary>
|
|
/// Tries to convert from input to output.
|
|
/// </summary>
|
|
/// <typeparam name="TInput"></typeparam>
|
|
/// <typeparam name="TOuptut"></typeparam>
|
|
/// <param name="input"></param>
|
|
/// <param name="result"></param>
|
|
/// <returns>True if everything was ok.</returns>
|
|
public delegate bool SafeConverter<TInput, TOuptut>(TInput input, out TOuptut result);
|
|
|
|
/// <summary>
|
|
/// Delegates helper.
|
|
/// </summary>
|
|
public static class Delegates
|
|
{
|
|
#region Parameter retainer
|
|
|
|
[Obsolete("Use a lambda expression directly")] // 2012.02
|
|
public static Action RetainParameter<TParameter>(TParameter value, Action<TParameter> action)
|
|
{
|
|
Throw.IfNull(action, "action");
|
|
return delegate
|
|
{
|
|
action(value);
|
|
};
|
|
}
|
|
|
|
/// <summary>
|
|
/// An non inderected provider.
|
|
/// </summary>
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <param name="item"></param>
|
|
/// <returns></returns>
|
|
public static Provider<T> Provider<T>(T item)
|
|
{
|
|
return delegate { return item; };
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region ExecuteProvider
|
|
|
|
/// <summary>
|
|
/// Converts a provider to an action.
|
|
/// </summary>
|
|
/// <param name="provider"></param>
|
|
/// <returns></returns>
|
|
public static Action ProviderToAction<TReturn>(Provider<TReturn> provider)
|
|
{
|
|
Throw.IfNull(provider, "provider");
|
|
|
|
return delegate
|
|
{
|
|
provider();
|
|
};
|
|
}
|
|
|
|
#endregion
|
|
|
|
public static void Execute(Action action)
|
|
{
|
|
Throw.IfNull(action, "action");
|
|
|
|
action();
|
|
}
|
|
|
|
public static void SafeExecute(Action action)
|
|
{
|
|
if (action != null)
|
|
{
|
|
action();
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// A provider that provides an object of type <typeparamref name="T"/>.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// Can be used a simple provider or as a factory.
|
|
/// </remarks>
|
|
/// <typeparam name="T">Type of the object to provide.</typeparam>
|
|
/// <returns>An object of type <typeparamref name="T"/>.</returns>
|
|
public delegate T Provider<T>();
|
|
|
|
/// <summary>
|
|
/// The <see cref="BinaryPredicate{T}"/> delegate type encapsulates a method that takes two
|
|
/// items of the same type, and returns a boolean value representating
|
|
/// some relationship between them. For example, checking whether two
|
|
/// items are equal or equivalent is one kind of binary predicate.
|
|
/// </summary>
|
|
/// <param name="item1">The first item.</param>
|
|
/// <param name="item2">The second item.</param>
|
|
/// <returns>Whether <paramref name="item1"/> and <paramref name="item2"/> satisfy the relationship that the BinaryPredicate defines.</returns>
|
|
public delegate bool BinaryPredicate<T>(T item1, T item2);
|
|
|
|
/// <summary>
|
|
/// The <see cref="BinaryPredicate{T1, T2}"/> delegate type encapsulates a method that takes two
|
|
/// items of different types, and returns a boolean value representating
|
|
/// some relationship between them.
|
|
/// </summary>
|
|
/// <param name="item1">The first item.</param>
|
|
/// <param name="item2">The second item.</param>
|
|
/// <returns>Whether <paramref name="item1"/> and <paramref name="item2"/> satisfy the relationship that the BinaryPredicate defines.</returns>
|
|
public delegate bool BinaryPredicate<T1, T2>(T1 item1, T2 item2);
|
|
|
|
[Obsolete("User System.Func instead")] // 2012.02
|
|
public delegate TReturn Functor<TReturn, TParameter1>(TParameter1 p1);
|
|
|
|
[Obsolete("User System.Func instead")] // 2012.02
|
|
public delegate TReturn Functor<TReturn, TParameter1, TParameter2>(TParameter1 p1, TParameter2 p2);
|
|
|
|
[Obsolete("User System.Func instead")] // 2012.02
|
|
public delegate TReturn Functor<TReturn, TParameter1, TParameter2, TParameter3>(TParameter1 p1, TParameter2 p2, TParameter3 p3);
|
|
|
|
[Obsolete("User System.Func instead")] // 2012.02
|
|
public delegate TReturn Functor<TReturn, TParameter1, TParameter2, TParameter3, TParameter4>(TParameter1 p1, TParameter2 p2, TParameter3 p3, TParameter4 p4);
|
|
|
|
/// <summary>
|
|
/// TryGetValue signature. (Returns true if the value can be retrieved, and assign the result with the result).
|
|
/// </summary>
|
|
/// <typeparam name="TKey"></typeparam>
|
|
/// <typeparam name="TValue"></typeparam>
|
|
/// <param name="key"></param>
|
|
/// <param name="result"></param>
|
|
/// <returns></returns>
|
|
public delegate bool TryGetValue<TKey, TValue>(TKey key, out TValue result);
|
|
}
|