Skip to content

Instantly share code, notes, and snippets.

@dokkillo
Created March 24, 2018 10:06
Show Gist options
  • Select an option

  • Save dokkillo/d481de8947e6ed6428ce974bbc865ed6 to your computer and use it in GitHub Desktop.

Select an option

Save dokkillo/d481de8947e6ed6428ce974bbc865ed6 to your computer and use it in GitHub Desktop.
Object Extension Methods
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace NominaWF.Utils.BasicMethods
{
static class Basic
{
internal static Func<object, double> ToDouble = (Obj) =>
{
if (null != Obj)
{
double valueCost = 0;
double.TryParse(Obj.ToString(), out valueCost);
return valueCost;
}
return 0;
};
internal static Func<object, int> ToInt = (Obj) =>
{
if (null != Obj)
{
int valueCost = 0;
int.TryParse(Obj.ToString(), out valueCost);
return valueCost;
}
return 0;
};
internal static Func<object, string> ToStringSecure = (Obj) =>
{
if(Obj != null)
{
return Obj.ToString();
}
return string.Empty;
};
internal static Func<object, bool> ToBool = (Obj) =>
{
if (Obj != null)
{
bool valueBool = false;
bool.TryParse(Obj.ToString(), out valueBool);
return valueBool;
}
return false;
};
internal static Func<object, DateTime> ToDate= (Obj) =>
{
DateTime date = DateTime.MinValue;
if (Obj != null)
{
DateTime.TryParse(Obj.ToString(), out date);
return date;
}
return date;
};
}
}
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
/// <summary>
/// Metodo de extension que comprueba que un valor es mayor de cero.
/// </summary>
/// <param name="Obj"></param>
/// <returns></returns>
public static bool IsMoreThanZero(this object Obj)
{
if (Obj != null)
{
double cost = Basic.ToDouble(Obj);
if (cost > 0)
{
return true;
}
}
return false;
}
/// <summary>
/// Metodo de extension que comprueba si un string esta vacio, es null o solo es un espacio
/// </summary>
/// <param name="Obj"></param>
/// <returns></returns>
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
/// <summary>
/// Metodo de extension que convierte un object a Double
/// </summary>
/// <param name="Obj"></param>
/// <returns></returns>
public static double ToDouble (this object Obj)
{
return Basic.ToDouble(Obj);
}
/// <summary>
/// Metodo de extension que convierte un object a Boolean
/// </summary>
/// <param name="Obj"></param>
/// <returns></returns>
public static bool ToBool(this object Obj)
{
return Basic.ToBool(Obj);
}
/// <summary>
/// Metodo de extension conversion Generica
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="Obj"></param>
/// <returns></returns>
public static T ToType<T>(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<T>(this object Obj)
{
if(typeof(T) == typeof(double))
{
return Math.Round(Obj.ToDouble(), 2, MidpointRounding.AwayFromZero).ToType<T>();
}
return Obj.ToType<T>();
}
/// <summary>
/// Metodo de extension que convierte un object a Int
/// </summary>
/// <param name="Obj"></param>
/// <returns></returns>
public static int ToInt(this object Obj)
{
return Basic.ToInt(Obj);
}
/// <summary>
/// Metodo de extension para comprobar que una variable es o no es Null antes de usar un .ToString()
/// </summary>
/// <param name="Obj"></param>
/// <returns></returns>
public static string ToStringSecure(this object Obj)
{
return Basic.ToStringSecure(Obj);
}
/// <summary>
/// Metodo de extension para convertir una fecha y formatearla con Padleft y dos ceros. Ej de 2 a 02
/// </summary>
/// <param name="Obj"></param>
/// <returns></returns>
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;
}
/// <summary>
/// Metodo de extension para convertir una fecha a la inversa YYYYMMDD
/// </summary>
/// <param name="Obj"></param>
/// <returns></returns>
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);
}
/// <summary>
/// Metodo de extension para devolver un numero con separador de decimales en . y no en ,
/// </summary>
/// <param name="Obj"></param>
/// <returns></returns>
public static string ToNumberStringDot(this object Obj)
{
var str = Basic.ToStringSecure(Obj);
return str.Replace(',', '.');
}
/// <summary>
/// Metodo de extension para devolver un numero con separador de decimales en , y no en .
/// </summary>
/// <param name="Obj"></param>
/// <returns></returns>
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
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment