How to get the properties of a type when using Generics C#

Asked

Viewed 368 times

6

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 her properties, but I’m not getting the way I want to:

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);

}
  • 1

    I don’t know if I can understand what the goal is. You want to instantiate a Pessoa within the method Inserir?

  • I guess now I understand, see if that’s what I wanted in my answer.

  • Do you want to create an instance of an object just to get its properties by Reflection? If so, it wouldn’t just be using one typeof(TEntidade).GetProperties()? I don’t see why you need the class instance there.

2 answers

8


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

I don’t know if you really need to do this, the code is so simple that you wouldn’t even need this method, let alone another class. Of course you may wish to make 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() => typeof(TEntidade).GetProperties();
}

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

  • Thanks buddy, that’s what I needed.

7

In fact it is not even necessary another method to do this, a simple GetProperties() using typeof would have done well.

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

Even if you want to follow the idea of generics, it is not necessary to pass this parameter since you already have the type in TEntidade.

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

Browser other questions tagged

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