Name of fields of a generic object - Reflection

Asked

Viewed 1,387 times

3

Good morning, everyone. I have a problem that I believe can be solved with Reflection, but I do not know how to use and I hope you can help me in 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 needs to go through it and identify the name of the fields, that is, it needs to discover that a field is called Id and the other is called Name. Example below:

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

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

Thanks.

  • As for the doubling of the question, I’m sorry, but I didn’t know how to look for the question, so I posted explaining exactly how I would need it. It was really bad.

2 answers

6


There is a class method 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)}");
    }
}

Full code for testing:

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 on . NET Fiddle.

  • Solved my problem. Thank you very, very much.

4

Basically, this would be the method to discover the names of fields of any type, example:

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

where items would be a list like string (List<string>) with all the names, in addition you can pick up values, types of each property, and the reflection should be used when really necessary, failed to say the focus to use this, but often that’s how it solves.

A reminder that pass an object the method DescobridorDeNome has been amended to that effect and is therefore a generic method.

It can also be made a code that for a certain type I know its properties, ie 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 instance example and only the type.

Full example:

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 and when to use, if you need the values of the fields (for example), you will need to use what has the class instance, now if you need to find out what has in that type use the second option.

References:

Browser other questions tagged

You are not signed in. Login or sign up in order to post.