using System;
using Chernobyl.Event;
using Chernobyl.Values;
namespace Chernobyl.Conversion
{
///
/// Converts a value contained within an to another
/// value and maintains that conversion.
///
/// The type to be converted.
/// The type to convert the value to.
public class FromSourceConversion : HeldValue
{
///
/// Initializes a new instance of the
/// class.
///
/// The instance whose contained value is to be
/// converted.
/// The method that performs the conversion.
public FromSourceConversion(IValue value, Func converter)
{
_converter = converter;
// Assign the event after configuring this instance in case the event
// is invoked immediately.
value.Provide += OnProvide;
}
///
/// Converts the value contained within the to
/// the value requested.
///
/// The sender of the event.
/// The
/// instance containing the event data.
void OnProvide(object sender, ValueChangedEventArgs e)
{
Value = _converter(e.NewValue);
}
///
/// The method that performs the conversion.
///
readonly Func _converter;
}
///
/// Utility and extensions methods for
/// and its dependencies.
///
public static class FromSourceConversionExtensions
{
///
/// Returns an that holds and maintains a
/// converted version of another .
///
/// The type to be converted.
/// The type to convert the value to.
/// The instance whose contained value is to be
/// converted.
/// The method that performs the conversion.
/// The which holds and maintains the
/// converted value.
public static IValue Convert(
this IValue value, Func converter)
{
return new FromSourceConversion(value, converter);
}
}
}