How to convert the fields to a row with string?

1

Follow the code:

Model:

public class Usuario
{
   public string Campo1 { get; set; }
   public string Campo2 { get; set; }
   public string Campo3 { get; set; }
   public string Campo4 { get; set; }
   public string Campo5 { get; set; }
   public string Campo6 { get; set; }
   public string Campo7 { get; set; }
   public string Campo8 { get; set; }
   public string Campo9 { get; set; }
   public string Campo10 { get; set; }
}

Controller:

var teste = new Usuario();
string texto = ""+teste.Campo1+ teste.Campo2+ etc"";

How can you see the above code, is there an easier way without typing teste ? That is, just type teste that already fills all fields with their value. Because tomorrow I can have more than 100 fields.

Any solution?

    
asked by anonymous 27.04.2017 / 20:42

4 answers

5

Considering that the number of fields will increase, use Reflection >:

public override string ToString()
{
    string retorno = "";
    foreach (var campo in this.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public |
                           BindingFlags.NonPublic | BindingFlags.FlattenHierarchy))
    {
        retorno += " " + campo.GetValue(this, null);
    }

   return retorno;
}

What I would do is define an ancestor class with the method:

public abstract class Basica 
{
   public override string ToString()
   {
        string retorno = "";
        foreach (var campo in this.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public |
                               BindingFlags.NonPublic | BindingFlags.FlattenHierarchy))
        {
            retorno += " " + campo.GetValue(this, null);
        }

       return retorno;
   }
}

Usuario inherit:

public class Usuario : Basica
{
   public string Campo1 { get; set; }
   public string Campo2 { get; set; }
   public string Campo3 { get; set; }
   public string Campo4 { get; set; }
   public string Campo5 { get; set; }
   public string Campo6 { get; set; }
   public string Campo7 { get; set; }
   public string Campo8 { get; set; }
   public string Campo9 { get; set; }
   public string Campo10 { get; set; }
}

And a test:

public class Program
{
    public static void Main()
    {
        var usuario = new Usuario 
        {
            Campo1 = "Teste1",
            Campo2 = "Teste2",
            Campo3 = "Teste3",
            Campo4 = "Teste4"
        };
        Console.WriteLine(usuario.ToString());
    }
}

I made a Fiddle .

    
27.04.2017 / 21:01
5

You can use a method or property in Model. For example:

public class Usuario
{
    public string Campo1{ get; set; }
    public string Campo2{ get; set; }
    public string Campo3{ get; set; }
    public string Campo4{ get; set; }
    //etc.

    public override string ToString()
    {
        return Campo1 + Campo2 + Campo3 + Campo4; // +etc.
    }
}

And in the controller:

var teste = new Usuario();
string texto = teste.ToString();

I have overloaded the ToString method of type, but you can also create a new method instead.

    
27.04.2017 / 20:49
5

You can do with reflection . You need to be careful because reflection often makes execution much slower.

See an example:

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

public class Program
{
    public static void Main()
    {
        var obj = new Usuario { Campo1 = "teste", Campo2 = "teste 2" };

        Type type = obj .GetType();

        var texto = "";
        foreach(var prop in obj.GetType().GetProperties()) 
        {
            Console.WriteLine($"{prop.Name}={prop.GetValue(obj, null)}");
            texto += prop.GetValue(obj, null) + " ";
        }
    }
}

public class Usuario
{
    public string Campo1 { get; set; }
    public string Campo2 { get; set; }
    public string Campo3 { get; set; }
    public string Campo4 { get; set; }
    public string Campo5 { get; set; }
    public string Campo6 { get; set; }
    public string Campo7 { get; set; }
    public string Campo8 { get; set; }
    public string Campo9 { get; set; }
    public string Campo10 { get; set; }
}

See working in .NET Fiddle.

    
27.04.2017 / 20:55
3

uses System.Reflection.PropertyInfo

overloads ToString () as Renan posted, but this way:

public override string ToString()
{
    string retorno = "";
    foreach (System.Reflection.PropertyInfo pr in this.GetType().GetProperties())
    {
        if (pr.CanRead)
        {
           object valor = pr.GetValue(this, null);
           retorno += pr.Name +": "+ (valor == null ? "" : valor.ToString()) + ", ";
        }
    }
    return retorno;
}

the result will be:

var teste = new Usuario();
string texto = teste.ToString();
//Campo1: Valor, Campo2: Valor, Campo3: Valor, ... 
    
27.04.2017 / 21:03