using System; using System.Collections.Generic; using System.Reflection; namespace UIWidgets { /// /// For each extensions. /// public static class Extensions { /// /// Foreach with index. /// /// Enumerable. /// Handler. /// The 1st type parameter. public static void ForEach(this IEnumerable enumerable, Action handler) { int i = 0; foreach (T item in enumerable) { handler(item, i); i++; } } /// /// Foreach. /// /// Enumerable. /// Handler. /// The 1st type parameter. public static void ForEach(this IEnumerable enumerable, Action handler) { foreach (T item in enumerable) { handler(item); } } /// /// Convert IEnumerable to ObservableList. /// /// The observable list. /// Enumerable. /// Is need to observe items? If true ObservableList.OnChange will be raised on item OnChange or PropertyChanged. /// The 1st type parameter. public static ObservableList ToObservableList(this IEnumerable enumerable, bool observeItems = true) { return new ObservableList(enumerable, observeItems); } /// /// Sums the float. /// /// The float. /// List. /// Calculate. /// The 1st type parameter. public static float SumFloat(this IList list, Func calculate) { var result = 0f; for (int i = 0; i < list.Count; i++) { result += calculate(list[i]); } return result; } /// /// Sums the float. /// /// The float. /// List. /// Calculate. /// The 1st type parameter. public static float SumFloat(this ObservableList list, Func calculate) { var result = 0f; for (int i = 0; i < list.Count; i++) { result += calculate(list[i]); } return result; } /// /// Convert the specified list with converter. /// /// Input. /// Converter. /// The 1st type parameter. /// The 2nd type parameter. static public List Convert(this List input, Converter converter) { #if NETFX_CORE var output = new List(input.Count); for (int i = 0; i < input.Count; i++) { output.Add(converter(input[i])); } return output; #else return input.ConvertAll(converter); #endif } #if NETFX_CORE /// /// Determines if is assignable from the specified source from. /// /// true if is assignable from the specified source from; otherwise, false. /// Source. /// From. static public bool IsAssignableFrom(this Type source, Type from) { return source.GetTypeInfo().IsAssignableFrom(from.GetTypeInfo()); } /// /// Apply action for each item in list. /// /// List. /// Action. /// The 1st type parameter. static public void ForEach(this List list, Action action) { for (int i = 0; i < list.Count; i++) { action(list[i]); } } #endif } }