Created
October 16, 2017 20:52
-
-
Save AlbertoMonteiro/354d8280538a083f08de75d376d5b4ff to your computer and use it in GitHub Desktop.
Descobrir propriedades string na arvore de um type
This file contains 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; | |
namespace ConsoleApp2 | |
{ | |
class Program | |
{ | |
private static readonly Type StringType = typeof(string); | |
static void Main(string[] args) | |
{ | |
ObterPropriedadesString(typeof(Pessoa)); | |
} | |
public static void ObterPropriedadesString(Type tipo) | |
{ | |
foreach (var propertyInfo in tipo.GetProperties()) | |
{ | |
if (propertyInfo.PropertyType == StringType) | |
Console.WriteLine($"Encontrado propriedade string de nome {propertyInfo.Name} em {tipo.FullName} "); | |
else if (propertyInfo.PropertyType.IsClass) | |
ObterPropriedadesString(propertyInfo.PropertyType); | |
} | |
} | |
} | |
class Pessoa | |
{ | |
public string Nome { get; set; } | |
public short Idade { get; set; } | |
public Carro Carro { get; set; } | |
} | |
class Carro | |
{ | |
public string Modelo { get; set; } | |
public Fabricante Fabricante { get; set; } | |
} | |
class Fabricante | |
{ | |
public string Nome { get; set; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment