fork download
  1. using static System.Console;
  2. using System.Reflection;
  3.  
  4. public class Program {
  5. public static void Main(string[] args) {
  6. var objGeneric = new Generica();
  7. objGeneric.Pessoa.Nome = "PAULO TADEU CHAGAS";
  8. objGeneric.Pessoa.Idade = 25;
  9. Teste(objGeneric.Pessoa);
  10. }
  11. static void Teste<T>(T xpto) {
  12. var tipo = xpto.GetType();
  13. PropertyInfo[] propt = tipo.GetProperties();
  14. foreach (var prop in tipo.GetProperties()) {
  15. WriteLine($"Nome: {prop.Name}");
  16. WriteLine($"Valor: {prop.GetValue(xpto, null)}");
  17. }
  18. }
  19. }
  20.  
  21. public class Generica {
  22. public Pessoa Pessoa { get; set; }
  23. public Generica() => this.Pessoa = new Pessoa();
  24. }
  25.  
  26. public class Pessoa {
  27. public string Nome { get; set; }
  28. public int Idade { get; set; }
  29. }
  30.  
  31. //https://pt.stackoverflow.com/q/405090/101
Success #stdin #stdout 0.02s 16492KB
stdin
Standard input is empty
stdout
Nome: Nome
Valor: PAULO TADEU CHAGAS
Nome: Idade
Valor: 25