using System; using System.Globalization; namespace Connected { public class Converter { public Func SetFunc { get; set; } public Func GetFunc { get; set; } /// /// The culture info being used for decimal points, date and time format, etc. /// public CultureInfo Culture { get; set; } = Converters.DefaultCulture; public Action OnError { get; set; } public bool SetError { get; set; } public bool GetError { get; set; } public string SetErrorMessage { get; set; } public string GetErrorMessage { get; set; } public U Set(T value) { SetError = false; SetErrorMessage = null; if (SetFunc == null) return default(U); try { return SetFunc(value); } catch (Exception e) { SetError = true; SetErrorMessage = $"Conversion from {typeof(T).Name} to {typeof(U).Name} failed: {e.Message}"; } return default(U); } public T Get(U value) { GetError = false; GetErrorMessage = null; if (GetFunc == null) return default(T); try { return GetFunc(value); } catch (Exception e) { GetError = true; GetErrorMessage = $"Conversion from {typeof(U).Name} to {typeof(T).Name} failed: {e.Message}"; } return default(T); } protected void UpdateSetError(string msg) { SetError = true; SetErrorMessage = msg; OnError?.Invoke(msg); } protected void UpdateGetError(string msg) { GetError = true; GetErrorMessage = msg; OnError?.Invoke(msg); } } /// /// Converter from T to string /// /// Set converts to string /// Get converts from string /// public class Converter : Converter { /// /// Custom Format to be applied on bidirectional way. /// public string Format { get; set; } = null; } }