Skip to content

Instantly share code, notes, and snippets.

@rdakar
rdakar / Validação de CPF e CNPJ - Java
Created September 18, 2017 20:42
Validação de CPF e CNPJ - Java
public class CpfCnpjUtils {
private static final int[] pesoCPF = {11, 10, 9, 8, 7, 6, 5, 4, 3, 2};
private static final int[] pesoCNPJ = {6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2};
public static boolean isValid(String cpfCnpj) {
return (isValidCPF(cpfCnpj) || isValidCNPJ(cpfCnpj));
}
private static int calcularDigito(String str, int[] peso) {
int soma = 0;
@rdakar
rdakar / Validação de CPF e CNPJ - C#
Created August 14, 2017 21:02
Validação de CPF e CNPJ - C#
namespace CpfCnpj
{
public static class CpfCnpjUtils
{
public static bool IsValid(string cpfCnpj)
{
return (IsCpf(cpfCnpj) || IsCnpj(cpfCnpj));
}
private static bool IsCpf(string cpf)
@rdakar
rdakar / EnumExtender.GetEnumValue
Created July 2, 2017 22:14
EnumExtender.GetEnumValue
public static T GetEnumValue<T>(this string description)
{
var type = typeof(T);
if (!type.GetTypeInfo().IsEnum)
throw new ArgumentException();
var field = type.GetFields().SelectMany(f => f.GetCustomAttributes(typeof(DescriptionAttribute), false), (f, a) => new { Field = f, Att = a })
.Where(a => ((DescriptionAttribute)a.Att).Description == description).SingleOrDefault();
return field == null ? default(T) : (T)field.Field.GetRawConstantValue();
}
@rdakar
rdakar / EnumExtender.GetDescription
Created June 27, 2017 23:23
EnumExtender.GetDescription
public static string GetDescription(this Enum enumerationValue)
{
Type type = enumerationValue.GetType();
MemberInfo member = type.GetMembers().Where(w => w.Name == Enum.GetName(type, enumerationValue)).FirstOrDefault();
var attribute = member?.GetCustomAttributes(typeof(DescriptionAttribute), false).FirstOrDefault() as DescriptionAttribute;
return attribute?.Description != null ? attribute.Description : enumerationValue.ToString();
}