How to iterate on a model in C# with variables that have number?

Asked

Viewed 89 times

-3

I have a model that was made by another developer where it has several fields like this:

. . .

Teste() teste = new Teste();
teste.campo_1 = 1;
teste.campo_2 = 2;
teste.campo_3 = 3;
teste.campo_4 = 4;
teste.campo_5 = 5;

How would you fill in the data using for?

  • 1

    I don’t see what this has to do with model, anyway, it’s simple, make it a list, array, or something like that, which is the right thing to do, after all it is clear that this is unique information with variants, it is the clear example that variables should be encapsulated into a single variable. If you can’t do this, then you’ll have to think about it, and it’ll be a huge gambit.

  • Model is a class no?! so it’s still the same...

  • 1

    @Ronaldoperes It’s not.

  • So this isn’t a model? class Data { public string dailyDealId { get; set; } public string discountPercentage { get; set; } public Array product { get; set; } }

2 answers

5

The right thing would be to turn this lot of properties into a collection (array, list or similar).

public class Teste
{
    public List<int> Campos { get; } = new List<int>();
}

So you don’t need to change the model every time you need a new field, plus it seems to make a lot more sense.

To use, you can do so:

var obj = new Teste();
for(int i = 0; i <= 8; i++)
{
    obj.Campos.Add(i);
}

See working on . NET Fiddle.

If you really can’t do this, you’ll have to use reflection, basically you’ll need the method SetValue of PropertyInfo.

For example:

var obj = new Teste();
string propBase = "campo_";

for(int i = 1; i <= 8; i++)
{
    var propName = propBase + i;
    typeof(Teste).GetProperty(propName).SetValue(obj, i, null);
}

See working on . NET Fiddle.

  • The first solution of the answer is correct, the second is gambiarra that solves when it is already wrong ;)

  • 1

    @Maniero Yes, it seems that the AP is more interested in the gambiarra than in the solution.

2


Like the Maniero said in his commenting, it is necessary to use reflection to do this. If you are unable to make the modifications he suggested, the following code works and can help you.

public class Teste
{
    public int campo_1 { get; set; }
    public int campo_2 { get; set; }
    public int campo_3 { get; set; }
    public int campo_4 { get; set; }
    public int campo_5 { get; set; }
}

public class Example
{
    public static void Main()
    {
        var teste = new Teste();
        var type = teste.GetType();
        var properties = type.GetProperties();

        foreach (var property in properties)
        {
            var valor = property.Name.Substring(property.Name.IndexOf("_") + 1);

            Console.WriteLine("Propriedade: " + property.Name);
            Console.WriteLine("Valor antes: " + property.GetValue(teste, null));

            property.SetValue(teste, Int32.Parse(valor));

            Console.WriteLine("Valor depois: " + property.GetValue(teste, null));
            Console.WriteLine();
        }
    }
}

Also see working here

  • Thank you, I think that’s right

Browser other questions tagged

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