Instantly share code, notes, and snippets.

View 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;
View 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)
View 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();
}
View 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();
}