How do I get properties of a type when I use Generics C #

4

I have the following class:

public class Pessoa
{
    public int id { get; set; }
    public string Nome { get; set; }
    public DateTime DataNascimento { get; set; }
}

I've developed a method to get the properties of it, but I can not use it the way I want:

public static class Entities<TEntidade> where TEntidade : class
{
    /* Assim da certo mas não quero passar por parametro */
    public static PropertyInfo[] GetPropertiesComoNaoQuero(TEntidade e)
    {         
        var entity = Activator.CreateInstance(e.GetType()) as TEntidade;
        PropertyInfo[] properties = entity.GetType().GetProperties();
        return properties;
    }

    public static PropertyInfo[] GetPropertiesComoEUQuero()
    {         
        /* 
         * Existe alguma forma de criar essa instancia sem passar por *parametro? so sabendo que o 
         * tipo generico meu é do tipo pessoa na chamada ? 
         */
        var entity = Activator.CreateInstance() as TEntidade;
        PropertyInfo[] properties = entity.GetType().GetProperties();
        return properties;
    }
}

public class Teste
{
    PropertyInfo[] properties = Entities<Pessoa>.GetPropertiesComoEUQuero();

    Pessoa pessa = new Pessoa();
    PropertyInfo[] propertiesb = Entities<Pessoa>.GetPropertiesComoNaoQuero(pessa);

}
    
asked by anonymous 11.05.2017 / 16:32

2 answers

6

It is possible using the typeof operator. See the difference for GetType() .

I do not know if you really need to do this, the code is so simple that you would not even need this method, let alone another class. Of course you may want to do an abstraction, but you must have a reason to do it.

using System;
using System.Reflection;

public class Program {
    public static void Main(string[] args) {
        var pessoa = new Pessoa { id = 1, Nome = "Nicola Bogar Uccio", DataNascimento = new DateTime(1988, 08, 24) };
        PropertyInfo[] properties = Entities<Pessoa>.GetPropertiesComoEUQuero();
    }
}

public class Pessoa {
    public int id { get; set; }
    public string Nome { get; set; }
    public DateTime DataNascimento { get; set; }
}

public static class Entities<TEntidade> where TEntidade : class {
    public static PropertyInfo[] GetPropertiesComoEUQuero() {         
        return typeof(TEntidade).GetProperties();
    }
}

See running on .NET Fiddle . And at Coding Ground . Also put it on GitHub for future reference .

    
11.05.2017 / 18:07
6

Actually, you do not even need another method to do this, a simple% wizard with% wizard will already work.

var properties = typeof(Pessoa).GetProperties();

Even if you want to follow the idea of generics, you do not need this parameter passing since you already have the type in GetProperties() .

public static class Entities<TEntidade> where TEntidade : class 
{
    public static PropertyInfo[] GetPropertiesComoEuQuero() 
    {         
        return typeof(TEntidade).GetProperties();
    }
}
    
11.05.2017 / 16:37