0

Im needing to convert an object structure to a simple dictionary(key, value)

My stucture is ex:

public abstract class Proposta
{
    public string CodigoProposta { get; set; }
    public Cliente Cliente { get; set; }
}


public class Cliente
{
    public string MatriculaCliente { get; set; }
    public Profissional Profissional { get; set; }
}

public class Profissional
{
    public string Profissao { get; set; }
    public string BairroComercial { get; set; }
    public string EnderecoComercial { get; set; }
    public string NumeroComercial { get; set; }
    public decimal RendaMensal { get; set; }
    public DateTime DatadeAdmissao { get; set; }
}

I need this to become a dictionary, like:

key, value
CodigoProposta , 1000
MatriculaCliente, 10
Profissao, "DEV"
BairroComercial  "Test"

ie building a dictionary with all the properties of the subclasses

I tried use this code for convert object to dictionary

How to convert object to Dictionary<TKey, TValue> in C#?


Example of class

public abstract class Proposta
{
    public string CodigoProposta { get; set; }
    public string NumeroContrato { get; set; }
    public string CodigoBeneficio { get; set; }
    public string CodigoOrgao { get; set; }
    public string CodigoConvenio { get; set; }
    public string CodigoProduto { get; set; }
    public string CodigoCorrespondente { get; set; }
    public string CodigoLoja { get; set; }
    public string CodigoUnidadeNegocio { get; set; }
    public Averbacao Averbacao { get; set; }
    public DigitadorProposta DigitadorProposta { get; set; }
    public DateTime DataRecebimentoProposta { get; set; } = DateTime.Now;
    public bool PropostaComSeguro { get; set; }
    public IEnumerable<Documento> Documentos { get; set; }
    public CanalVenda CanalVenda { get; set; }
    public Cliente Cliente { get; set; }
    public Situacao Situacao { get; set; }
    public bool ClienteFolhaDePagamentoNoSantander { get; set; }
    public string EmailResponsavelProposta { get; set; }
    public Produto Produto { get; set; }
    public Esteira Esteira { get; set; }
}

public class Averbacao
{
    public string NumeroReservaMargem { get; set; }
    public decimal ValorMargem { get; set; }
    public string CodigoAutenticacao { get; set; }
    public decimal ValorAverbado { get; set; }
    public string TipoDeAverbacaoDoConvenio { get; set; }
}

public class Cliente
{
    public string MatriculaCliente { get; set; }
    public string CpfCliente { get; set; }
    public string Nome { get; set; }
    public DateTime DataNascimento { get; set; }
    public Sexo Sexo { get; set; }
    public EstadoCivil EstadoCivil { get; set; }
    public string NomeMae { get; set; }
    public string Email { get; set; }
    public string Naturalidade { get; set; }
    public Nacionalidade Nacionalidade { get; set; }
    public bool PessoaPoliticamenteExposta { get; set; }
    public bool CorrentistaSantander { get; set; }
    public Identidade Identidade { get; set; }
    public Endereco EnderecoResidencial { get; set; }
    public IEnumerable<Telefone> Telefones { get; set; }
    public decimal RendaMensal { get; set; }
    public Profissional Profissional { get; set; }
}

public class Telefone
{
    public TipoTelefone Tipo { get; set; }
    public int DDD { get; set; }
    public int Numero { get; set; }
}
Ryan M
  • 18,333
  • 31
  • 67
  • 74
  • What will you then do with your new dictionary? – Caius Jard Mar 30 '22 at 20:43
  • Are those the final versions of your classes? Will any of your classes contain arrays, lists, or other sequence types? If so, how should they be serialized to the dictionary (specifically, what should their keys be)? Is there any chance that a property will be a reference to an object above it in the object tree? – Mike Hofer Mar 30 '22 at 20:45
  • @CaiusJard i need this date I need the data in this format, to integrate with a system – Bruno Oliveira Mar 30 '22 at 20:49
  • @MikeHofer No, my classes are bigger. yes, have some list, i will add a example more detailed for u understand – Bruno Oliveira Mar 30 '22 at 20:52
  • Integrate how ? – Caius Jard Mar 30 '22 at 23:09
  • Why do you want to do this, its seems like an XY problem. https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem If you want to accees fields by their name just use reflection. – pm100 Mar 31 '22 at 00:09
  • What kind of integration is this? what data format is this other system expecting? is it an API? do you need to send then a file in a specific format? – Elton Santana Mar 31 '22 at 01:46
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Mar 31 '22 at 01:46

0 Answers0