Last active
November 25, 2022 22:04
-
-
Save bennotti/1609f56cd6f49d0128616f58017edacc to your computer and use it in GitHub Desktop.
lista campos/propriedades de uma classe
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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 = "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.Number); | |
| }*/ | |
| var y = new Teste { | |
| Nome = "ok", | |
| Valor = "2" | |
| }; | |
| var camposSelect = CamposSelect<Teste>(new { | |
| Nome = "", | |
| casa = 2, | |
| Email = "" | |
| }); | |
| //await insert( | |
| // NOME | EMAIL, | |
| // new { nome = "", email = "" }); | |
| //await inserir(new { nome = "", email = "" }); | |
| foreach(var campo in camposSelect) | |
| { | |
| Console.WriteLine(campo); | |
| } | |
| 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) | |
| { | |
| 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; | |
| } | |
| } | |
| } | |
| } | |
| return propNames.ToArray(); | |
| } | |
| public class Teste: Teste2 | |
| { | |
| [Coluna] | |
| public string Nome { get; set; } | |
| [Coluna("ValorAmount")] | |
| public string Valor { get; set; } | |
| } | |
| public class Teste2 | |
| { | |
| [Coluna("EMAIL_CAMPO")] | |
| public string Email { get; set; } | |
| } | |
| [System.AttributeUsage(System.AttributeTargets.Property) | |
| ] | |
| public class ColunaAttribute : System.Attribute | |
| { | |
| 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) | |
| { | |
| UsarInsert = usarInsert; | |
| UsarSelect = usarSelect; | |
| UsarUpdate = usarUpdate; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment