How to convert fields to a line with string?

Asked

Viewed 69 times

1

Follows 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"";

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

Some solution?

4 answers

5

You can use a method or property in the 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 on the controller:

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

I overloaded the type Tostring method, but you can also create a new method instead.

5

You can do with Reflection. You need to be careful because usually, Reflection leaves execution much longer.

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

  • Interesting your answer, thank you jbueno.

5


Whereas 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 ancestral 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.

  • 1

    Solved problem here, thank you Gypsy Morrisson Mendez

3

uses System.Reflection.Propertyinfo

overloads Tostring() as Renan posted, but in that 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;
}

then the result will be:

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

Browser other questions tagged

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