Get type of an entity framework property

4

Good morning,

You would need to obtain, at run time, the corresponding property data type:

foreach(string  unaPropiedad in listColumnNames)
{
   clsEntidad.getype().getproperty(unaPropiedad);
   PropertyInfo prop= prop.propertyType;
   Type tipo= Type.GetTypeCode(prop);
 }
    switch (tipo)
    {
        case TypeCode.Byte:
       //-------

}

When I consult the type in switch I always receive the base type Object not the type of the property in question. Any idea or alternative to focus this?

Thank you.

    
asked by Kram_ 23.08.2016 в 17:51
source

3 answers

2

The following code works correctly for me

public class clsEntidad
{
    public byte prop1 { get; set; }
    public string prop2 { get; set; }
}


private void TestReflection()
{
    string[] listColumnNames = new string[] { "prop1", "prop2" };

    clsEntidad entity = new clsEntidad();

    foreach (string unaPropiedad in listColumnNames)
    {
        var prop = entity.GetType().GetProperty(unaPropiedad);
        //PropertyInfo prop = prop.PropertyType;
        TypeCode typeCode = Type.GetTypeCode(prop.PropertyType);
        switch (typeCode)
        {
            case TypeCode.Byte:
                //codigo
                break;

        }
    }

}

You'll see that from the Type you get the TypeCode

    
answered by 23.08.2016 в 18:31
2

In your case I would solve it this way:

This line clsEntidad.getype().getproperty(unaPropiedad); should replace it with:

var prop = typeof(clsEntidad).GetProperty(unaPropiedad);

You are using the enumerated TypeCode to identify them in the switch correctly only that you assign a value int to a variable of type Type in the line: Type tipo = Type.GetTypeCode(prop);

Assuming a class:

    public class clsEntidad
    {
        public int Propiead1 { get; set; }
        public decimal Propiedad2 { get; set; }
    }

the code would look like this:

        string[] listColumnNames = new string[] { "Propiead1", "Propiedad2" };

        foreach (string unaPropiedad in listColumnNames)
        {
            var prop = typeof(clsEntidad).GetProperty(unaPropiedad);
            TypeCode tipo = Type.GetTypeCode(prop.PropertyType);

            switch (tipo)
            {
                case TypeCode.Int32:
                    Console.WriteLine("Propiedad del Tipo: {0}",tipo);
                    break;
                case TypeCode.Decimal:
                    Console.WriteLine("Propiedad del Tipo: {0} ",tipo);
                    break;
                default:
                    break;
            }
        }

        Console.ReadKey();

To get the properties of a class and its types you can do it this way (it's a console project for a matter of speed but I hope it works for you):

class Program
{
    public int MyProperty1 { get; set; }
    public short MyProperty2 { get; set; }
    public decimal MyProperty3 { get; set; }

    static void Main(string[] args)
    {
        foreach (var t in typeof(Program).GetProperties())
        {
            Console.WriteLine("{0} ", t.PropertyType.FullName);
            Console.WriteLine();
        }

        Console.ReadKey();
    }
}

and the output would be:

  

System.Int32

     

System.Int16

     

System.Decimal

    
answered by 23.08.2016 в 18:43
0

Exactly, very similar is the code I use but what I'm surprised about is that I get the switch type object in all the iterated properties, not the types int32, datetime etc ...

    
answered by 23.08.2016 в 18:35