Getvalue() does not show value that is assigned to the variable

Asked

Viewed 107 times

0

        static void Main(string[] args)
    {

       // ASSIM FUNCIONA
        //var ObjGeneric = new Pessoa();
        //ObjGeneric.Nome = "PAULO TADEU CHAGAS";
        //ObjGeneric.Idade = 25;

         // ASSIM NÃO FUNCIONA 
        //CHAMADA ATRAVÉS DA CLASSE GENERICA NÃO IMPRIME VALOR DA VARIAVEL
        var ObjGeneric = new Generica();
        ObjGeneric.Pessoa.Nome = "PAULO TADEU CHAGAS";
        ObjGeneric.Pessoa.Idade = 25;

        Teste(ObjGeneric);
    }


    static void Teste<T>(T xpto)
    {
        var tipo = xpto.GetType();

        PropertyInfo[] propt = tipo.GetProperties();

        foreach (var prop in tipo.GetProperties())
        {
            Console.WriteLine("Nome: " + prop.Name);
            Console.WriteLine("Valor: " + prop.GetValue(xpto, null));
            Console.ReadKey();
        }

    }

public class Generica
{
    public Pessoa Pessoa { get; set; }

    public Generica()
    {
        this.Pessoa = new Pessoa();
    }
}
public class Pessoa
{
    public string Nome { get; set; }
    public int Idade { get; set; }
}
  • 1

    Missing important snippets there to test and see the error, and even more accurate information about what happens or what it wants.

  • Enter the class declaration Generica in your question. Without this definition, speculation can only be made about the behavior of your code. Speculations of the type: properties do not appear in reflection because they are deprived or protected? Or: Nome and Valor are not properties, are fields and would have to be searched with GetFields()? Still: There is some metadata modifying its properties so that they cannot be displayed by reflection?....

  • Follow the class definition, yesterday I could not edit the question. When I invoke the method only using the Person class works it prints the name and age. Now when I call through the Generic class it prints the name property but the age value does not come out.

  • Are you sure you want to do this? This code makes no sense.

  • Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful for you. You can also vote on any question or answer you find useful on the entire site (when you have 15 points).

1 answer

3

Reflection is often abused, in rare cases it is really useful and has a great cost of performance, and only this should make you wonder if you should use, apart from the probable problem of robustness. There are cases to use, but if it is to save typing is not a good idea, have better options. This case does unnecessary things.

It was not easy to understand what I wanted because even trying to put more code did not give accurate information about what you want (as requested). Programming is understanding the problem first, the easy part is coding. When you can’t tell other people what you want is because even you don’t know very well what you want and you should pay more attention to it before coding, this is called the rubber duck (That’s silly, but the recommendation is good).

The problem is not even of reflection is to pass what you want, is passing an object that has a property and is printing it. Since the type is by reference the default is that her text is the name of the type. If that’s what you wanted there’s nothing wrong. As you are saying that you wanted me to print the values that are within this property then you must pass this property and not the object. So I passed objGeneric.Pessoa:

using static System.Console;
using System.Reflection;

public class Program {
    public static void Main(string[] args) {
        var objGeneric = new Generica();
        objGeneric.Pessoa.Nome = "PAULO TADEU CHAGAS";
        objGeneric.Pessoa.Idade = 25;
        Teste(objGeneric.Pessoa);
    }
    static void Teste<T>(T xpto) {
        var tipo = xpto.GetType();
        PropertyInfo[] propt = tipo.GetProperties();
        foreach (var prop in tipo.GetProperties()) {
            WriteLine($"Nome: {prop.Name}");
            WriteLine($"Valor: {prop.GetValue(xpto, null)}");
        }
    }
}

public class Generica {
    public Pessoa Pessoa { get; set; }
    public Generica() => this.Pessoa = new Pessoa();
}

public class Pessoa {
    public string Nome { get; set; }
    public int Idade { get; set; }
}

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

Of course, you might want me to take this property and the properties of the object that are inside it. But the question says nothing about it, so I’m giving you the simplest possible solution. If you want to take what is inside the object also have to make a code that does this, probably recursive. Only that you have to be careful, you can generate a very large and even cyclic graph, and without a clear and implemented rule to break it the code will crash. It’s extremely complicated to do right and it’s almost always the wrong technique, so don’t.

  • Thanks for the feedback, clarified my doubt. It worked =)

  • @Paulotadeuchagas see the [tour] the best way to say thank you.

Browser other questions tagged

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