Reflection

CSharp · Console

/Program.cs

using System.Reflection;
using System.Linq;
using System;

public class Program
{
	public static void Main()
	{
		 Carro car = new Carro
        {
            CarroId = 1,
            Modelo = "Fiesta",
            Marca = "Ford"
        };

        var properties = Service.GetProperties(car);
  		
		var atributo = "Modelo";

        var atributoSelecionado = properties.Where(p => p.Name == atributo).FirstOrDefault().GetValue(car,null);
		
		Console.WriteLine(atributoSelecionado);
		
        foreach (var p in properties)
        {
            string name = p.Name;
            var value = p.GetValue(car, null);
			//Console.WriteLine("Nome: {0} - Valor: {1}", name, value);
        }				        
	}
}



public class Carro
{
    public int CarroId { get; set; }
    public string Modelo { get; set; }
    public string Marca { get; set; }
}

public static class Service
{
    public static PropertyInfo[] GetProperties(object obj)
    {
        return obj.GetType().GetProperties();
    }
}
;