Field name of a generic object - reflection [duplicate]

3

Good morning everyone. I have a problem that I believe can be solved with Reflection, but I do not know how to use it and hope they can help me through my difficulty.

I have an object that will be passed as a parameter to a specific method. This method should be able to read this generic object and identify the name of all fields of this object. Below the example.

public class Aluno
{
    public int Id {get;set;}
    public string Nome {get;set;}
}

The method below will receive this object and need to go through it and identify the name of the fields, that is, it needs to find that one field is called Id and the other one is named Name . Example below:

public void DescobridorDeNome(T Objeto)
{
    //Aqui ele vai descobrir o nome dos campos do objeto.
}

So in this method I believe that using Reflection will solve. Does anyone know how I can do this?

Thanks.

    
asked by anonymous 12.12.2017 / 14:20

2 answers

6

There is a method of class Type called GetProperties

public static void DescobridorDeNome<T>(T objeto)
{
    var props = objeto.GetType().GetProperties();

    foreach(var prop in props)
    {
        Console.WriteLine($"{prop.Name} = {prop.GetValue(objeto, null)}");
    }
}

Complete test code:

using System;

public class Program
{
    public static void Main()
    { 
        var aluno = new Aluno{ Id = 1, Nome = "LINQ" };
        DescobridorDeNome(aluno);
    }

    public static void DescobridorDeNome<T>(T objeto)
    {
        var props = objeto.GetType().GetProperties();

        foreach(var prop in props)
        {
            Console.WriteLine($"{prop.Name} = {prop.GetValue(objeto, null)}");
        }
    }
}

public class Aluno
{
    public int Id {get;set;}
    public string Nome {get;set;}
}

See working in .NET Fiddle.

    
12.12.2017 / 14:29
4

Basically, this would be the method for finding field names of any type, eg

void DescobridorDeNome<T>(T objeto)
{
    var items = objeto.GetType()
            .GetProperties()
            .Select(x => x.Name)
            .ToList();
}

where items would be a list of type string ( List<string> ) with all names, in addition you can get values, types of each property, and reflection should be used when really needed, the focus to use this, but often that's the way it works out.

A reminder that passing an object to the DescobridorDeNome method has been changed to this, and so a generic method is used to do this.

I can also make a code that for a certain type I know its properties, that is, not needing the instance of an object, just pass the type, example :

static void DescobridorDeNomeGetType(Type t)
{
    var items = t.GetProperties()
            .Select(x => x.Name)
            .ToList();
}

An example with instance and only type .

class Program
{
    static void DescobridorDeNome<T>(T objeto)
    {
        var items = objeto.GetType()
            .GetProperties()
            .Select(x => x.Name)
            .ToList();


        foreach (string name in items)
            Console.WriteLine(name);
    }

    static void DescobridorDeNomeGetType(Type t)
    {
        var items = t.GetProperties()
            .Select(x => x.Name)
            .ToList();

        foreach (var name in items)
            Console.WriteLine(name);
    }

    static void Main(string[] args)
    {
        // com instância
        DescobridorDeNome(new Aluno());

        // só o tipo
        DescobridorDeNomeGetType(typeof(Aluno));
    }
}

Then everything will depend on what time to use, if you need the field values ( for example ), you will need to use what has the instance of the class, now if you need to find out what you have in that type use the second option.

12.12.2017 / 14:29