how to traverse attributes of a class in C #?

4

I have several classes with different attributes, one of them for example is:

class MGestion
{
    public int age { get; set; }
    public String gestion { get; set; }
    public String inicio { get; set; }
    public String fin { get; set; }
    public String descripcion { get; set; }
    public MGestion(int a,String g,String i,String f,String d) {
        age = a;
        gestion = g;
        inicio = i;
        fin = f;
        descripcion = d;
    }
}

What I want to do is to go through these attributes as they are done with a array and get the name and data type of each of them.

That's saying something like this:

  

name: age, data type : int
name: gestion, data type : String
  .. and so with everyone

I do not know if it was possible. Thanks for any help.

    
asked by Shassain 29.03.2018 в 20:19
source

1 answer

7

To get the names and types of each field in your class, you only need to use the operator typeof and one or two things.

The examples that I will put now, make use of the class System.Console to show results on screen.

System.Type only stores information about the type to which it belongs, that is, its own class.

Method 1: Get names and types of fields in a class.

Now, we assume with the class that you have put us in the example, I have made some modifications so that you can do what you need in the simple way to understand:

public class MGestion
{
    public int age;
    public String gestion;
    public String inicio;
    public String fin;
    public String descripcion;
}

Within the function you want to use to print members, you only need to do the following:

using System;
using System.Reflection;

// Namespace, class Program, class MGestion, etc...
public static void Main()
{
    Type Datos = typeof (MGestion);
    Console.WriteLine("Los campos de la clase '" + Datos.Name + "', son:");
    foreach (FieldInfo F 
         in Datos.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)) // Aqui ocurre la magia :)
    {
        Console.WriteLine("Nombre: '{0,-12}', Tipo: '{1,-10}'", F.Name, F.FieldType.Name);
    }
}

This will search all the fields public , private and not static due to the BindingFlags that I put in the function.

The code above is going to throw something like:

Los campos de la clase 'MGestion', son:
Nombre: 'age         ', Tipo: 'Int32     '
Nombre: 'gestion     ', Tipo: 'String    '
Nombre: 'inicio      ', Tipo: 'String    '
Nombre: 'fin         ', Tipo: 'String    '
Nombre: 'descripcion ', Tipo: 'String    '

Method 2: Get names and type of properties in a class.

As we know, fields and properties are two different things, mysteriously, the properties are also variable, but more beautiful and that.

To get the list of properties and print their value, we simply have to change two things to the way we use above to make it look like this:

foreach (PropertyInfo F 
     in Datos.GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)) // Aqui ocurre la magia :)
{
    Console.WriteLine("Nombre: '{0,-12}', Tipo: '{1,-10}'", F.Name, F.PropertyType);
}

In this method we only look for the properties of the class, whether they are public or private and that they are not static , tested with the following class:

public class MGestion
{
    public int age { get; set; }
    public String gestion { get; set; } 
    public String inicio { get; set; } 
    public String fin { get; set; } 
    public String descripcion { get; set; } 
    private int x { get; set; } 
    public int a;
    public int b;
    public string c;
}

Display something like the following on the screen:

Los campos de la clase 'MGestion', son:
Nombre: 'a           ', Tipo: 'Int32     '
Nombre: 'b           ', Tipo: 'Int32     '
Nombre: 'c           ', Tipo: 'String    '

Las propiedades de la clase 'MGestion' son:
Nombre: 'age         ', Tipo: 'System.Int32'
Nombre: 'gestion     ', Tipo: 'System.String'
Nombre: 'inicio      ', Tipo: 'System.String'
Nombre: 'fin         ', Tipo: 'System.String'
Nombre: 'descripcion ', Tipo: 'System.String'
Nombre: 'x           ', Tipo: 'System.Int32'

I hope it has helped you, here I leave you a fiddle so you can see how it works: D

    
answered by 29.03.2018 / 20:56
source