using System; using System.Collections.Generic; using System.Linq; using System.Text; using NominaWF.Utils.BasicMethods; using Nomina.WF.Constants; namespace NominaWF.Utils.Extension { public static class ObjectExtension { #region Validations /// /// Metodo de extension que comprueba que un valor es mayor de cero. /// /// /// public static bool IsMoreThanZero(this object Obj) { if (Obj != null) { double cost = Basic.ToDouble(Obj); if (cost > 0) { return true; } } return false; } /// /// Metodo de extension que comprueba si un string esta vacio, es null o solo es un espacio /// /// /// public static bool IsStringEmpty(this object Obj) { var value = Obj.ToStringSecure(); if (String.IsNullOrEmpty(value) || string.IsNullOrWhiteSpace(value)) { return true; } return false; } #endregion #region Format /// /// Metodo de extension que convierte un object a Double /// /// /// public static double ToDouble (this object Obj) { return Basic.ToDouble(Obj); } /// /// Metodo de extension que convierte un object a Boolean /// /// /// public static bool ToBool(this object Obj) { return Basic.ToBool(Obj); } /// /// Metodo de extension conversion Generica /// /// /// /// public static T ToType(this object Obj) { if (Obj is T) { return (T)Obj; } else { try { return (T)Convert.ChangeType(Obj, typeof(T)); } catch (InvalidCastException ex) { if (Obj == null) { return default(T); } throw ex; } } } public static T ToRound(this object Obj) { if(typeof(T) == typeof(double)) { return Math.Round(Obj.ToDouble(), 2, MidpointRounding.AwayFromZero).ToType(); } return Obj.ToType(); } /// /// Metodo de extension que convierte un object a Int /// /// /// public static int ToInt(this object Obj) { return Basic.ToInt(Obj); } /// /// Metodo de extension para comprobar que una variable es o no es Null antes de usar un .ToString() /// /// /// public static string ToStringSecure(this object Obj) { return Basic.ToStringSecure(Obj); } /// /// Metodo de extension para convertir una fecha y formatearla con Padleft y dos ceros. Ej de 2 a 02 /// /// /// public static string ToStringDate(this object Obj) { var date = Basic.ToStringSecure(Obj); if(date != string.Empty) { return date.PadLeft(2, '0'); } NominaWFErrors error = new NominaWFErrors(Constant.DayOrMonthFormatIncorrect(date)); throw error; } /// /// Metodo de extension para convertir una fecha a la inversa YYYYMMDD /// /// /// public static string ToInverseDate(this object Obj) { var date = ToDate(Obj); return date.Year + date.Month.ToString().PadLeft(2, '0') + date.Day.ToString().PadLeft(2, '0'); } public static DateTime ToDate(this object Obj) { return Basic.ToDate(Obj); } /// /// Metodo de extension para devolver un numero con separador de decimales en . y no en , /// /// /// public static string ToNumberStringDot(this object Obj) { var str = Basic.ToStringSecure(Obj); return str.Replace(',', '.'); } /// /// Metodo de extension para devolver un numero con separador de decimales en , y no en . /// /// /// public static string ToNumberStringComma(this object Obj) { var str = Basic.ToStringSecure(Obj); return str.Replace('.', ','); } public static string ToCSVString(this object Obj) { var str = Basic.ToStringSecure(Obj); return "\"" + str + "\""; } #endregion } }