Skip to content

Instantly share code, notes, and snippets.

@bennotti
Last active November 25, 2022 22:04
Show Gist options
  • Save bennotti/1609f56cd6f49d0128616f58017edacc to your computer and use it in GitHub Desktop.
Save bennotti/1609f56cd6f49d0128616f58017edacc to your computer and use it in GitHub Desktop.

Revisions

  1. bennotti revised this gist Nov 25, 2022. 1 changed file with 47 additions and 25 deletions.
    72 changes: 47 additions & 25 deletions program.cs
    Original file line number Diff line number Diff line change
    @@ -3,25 +3,26 @@
    using System.Reflection;
    using System.Reflection.Metadata;


    var padrao = new List<padraoDto> { new padraoDto { Number = "208"}, new padraoDto { Number = "655" } };
    var cliente = new List<string> { "655" };
    /*
    var padrao = new List<padraoDto> { new padraoDto { Number = "234"}, new padraoDto { Number = "754" } };
    var cliente = new List<string> { "754" };
    var listaFaltando = padrao.Where(p => !cliente.Any(x => x == p.Number)).ToList();
    foreach (var item in listaFaltando)
    {
    Console.WriteLine(item);
    }
    Console.WriteLine(item.Number);
    }*/

    var y = new Teste {
    Nome = "ok",
    Valor = "2"
    };

    var camposSelect = CamposSelect(new {
    nome = "",
    casa = 2
    var camposSelect = CamposSelect<Teste>(new {
    Nome = "",
    casa = 2,
    Email = ""
    });

    //await insert(
    @@ -35,50 +36,71 @@
    Console.WriteLine(campo);
    }

    static string[] CamposSelect(object atype)
    static string[] CamposSelect<T>(object atype) where T : class
    {
    if (atype == null) return new string[] { };
    Type t = atype.GetType();
    Type entityType = typeof(T);
    PropertyInfo[] entityProps = entityType.GetProperties();
    PropertyInfo[] props = t.GetProperties();
    List<string> propNames = new List<string>();
    foreach (PropertyInfo prp in props)
    {
    var nomeCampo = prp.Name;
    object[] attrs = prp.GetCustomAttributes(true);
    foreach (object attr in attrs)
    {
    UsarSelectAttribute selectAttr = attr as UsarSelectAttribute;
    if (selectAttr != null && !string.IsNullOrEmpty(selectAttr.NomeCampo)) {
    nomeCampo = selectAttr.NomeCampo;
    foreach (PropertyInfo prpEntity in entityProps) {
    if (prp.Name == prpEntity.Name) {
    var nomeCampo = string.Empty;
    object[] attrs = prpEntity.GetCustomAttributes(true);
    foreach (object attr in attrs) {
    ColunaAttribute selectAttr = attr as ColunaAttribute;
    if (selectAttr != null && selectAttr.UsarSelect) {
    nomeCampo = string.IsNullOrEmpty(selectAttr.NomeCampo) ? prp.Name : selectAttr.NomeCampo;
    }
    }
    if (!string.IsNullOrEmpty(nomeCampo)) {
    propNames.Add(nomeCampo);
    break;
    }
    }
    }
    propNames.Add(nomeCampo);

    }
    return propNames.ToArray();
    }

    public class Teste: Teste2
    {
    [UsarSelect]
    [Coluna]
    public string Nome { get; set; }
    [UsarSelect("ValorAmount")]
    [Coluna("ValorAmount")]
    public string Valor { get; set; }
    }

    public class Teste2
    {
    [UsarSelect("EMAIL_CAMPO")]
    [Coluna("EMAIL_CAMPO")]
    public string Email { get; set; }
    }

    [System.AttributeUsage(System.AttributeTargets.Property)
    ]
    public class UsarSelectAttribute : System.Attribute
    public class ColunaAttribute : System.Attribute
    {
    public string NomeCampo { get; private set; }
    public UsarSelectAttribute() { }
    public UsarSelectAttribute(string nomeCampo)
    public string NomeCampo { get; private set; } = string.Empty;
    public bool UsarSelect { get; private set; } = true;
    public bool UsarInsert { get; private set; } = true;
    public bool UsarUpdate { get; private set; } = true;
    public ColunaAttribute() { }
    public ColunaAttribute(string nomeCampo = "", bool usarSelect = true, bool usarInsert = true, bool usarUpdate = true)
    {
    UsarInsert = usarInsert;
    NomeCampo = nomeCampo;
    UsarSelect = usarSelect;
    UsarUpdate = usarUpdate;
    }
    public ColunaAttribute(bool usarSelect = true, bool usarInsert = true, bool usarUpdate = true)
    {
    this.NomeCampo = nomeCampo;
    UsarInsert = usarInsert;
    UsarSelect = usarSelect;
    UsarUpdate = usarUpdate;
    }
    }
  2. bennotti revised this gist Nov 24, 2022. 2 changed files with 35 additions and 1 deletion.
    13 changes: 13 additions & 0 deletions padraoDto.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace PocClassPropertiesList
    {
    public class padraoDto
    {
    public string Number { get; set; }
    }
    }
    23 changes: 22 additions & 1 deletion program.cs
    Original file line number Diff line number Diff line change
    @@ -1,13 +1,34 @@
    // See https://aka.ms/new-console-template for more information
    using PocClassPropertiesList;
    using System.Reflection;
    using System.Reflection.Metadata;


    var padrao = new List<padraoDto> { new padraoDto { Number = "208"}, new padraoDto { Number = "655" } };
    var cliente = new List<string> { "655" };

    var listaFaltando = padrao.Where(p => !cliente.Any(x => x == p.Number)).ToList();

    foreach (var item in listaFaltando)
    {
    Console.WriteLine(item);
    }

    var y = new Teste {
    Nome = "ok",
    Valor = "2"
    };

    var camposSelect = CamposSelect(y);
    var camposSelect = CamposSelect(new {
    nome = "",
    casa = 2
    });

    //await insert(
    // NOME | EMAIL,
    // new { nome = "", email = "" });

    //await inserir(new { nome = "", email = "" });

    foreach(var campo in camposSelect)
    {
  3. bennotti created this gist Nov 23, 2022.
    63 changes: 63 additions & 0 deletions program.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,63 @@
    // See https://aka.ms/new-console-template for more information
    using System.Reflection;
    using System.Reflection.Metadata;

    var y = new Teste {
    Nome = "ok",
    Valor = "2"
    };

    var camposSelect = CamposSelect(y);

    foreach(var campo in camposSelect)
    {
    Console.WriteLine(campo);
    }

    static string[] CamposSelect(object atype)
    {
    if (atype == null) return new string[] { };
    Type t = atype.GetType();
    PropertyInfo[] props = t.GetProperties();
    List<string> propNames = new List<string>();
    foreach (PropertyInfo prp in props)
    {
    var nomeCampo = prp.Name;
    object[] attrs = prp.GetCustomAttributes(true);
    foreach (object attr in attrs)
    {
    UsarSelectAttribute selectAttr = attr as UsarSelectAttribute;
    if (selectAttr != null && !string.IsNullOrEmpty(selectAttr.NomeCampo)) {
    nomeCampo = selectAttr.NomeCampo;
    }
    }
    propNames.Add(nomeCampo);
    }
    return propNames.ToArray();
    }

    public class Teste: Teste2
    {
    [UsarSelect]
    public string Nome { get; set; }
    [UsarSelect("ValorAmount")]
    public string Valor { get; set; }
    }

    public class Teste2
    {
    [UsarSelect("EMAIL_CAMPO")]
    public string Email { get; set; }
    }

    [System.AttributeUsage(System.AttributeTargets.Property)
    ]
    public class UsarSelectAttribute : System.Attribute
    {
    public string NomeCampo { get; private set; }
    public UsarSelectAttribute() { }
    public UsarSelectAttribute(string nomeCampo)
    {
    this.NomeCampo = nomeCampo;
    }
    }