samedi 23 décembre 2017

Geting property type with reflection

I have a little problem by getting the type of an Property by reflection.

I have a class which include only simple types like string, int, decimal...

public class simple 
{
  public string article { get; set; }
  public decimal price { get; set; }
}

Now I need to take this properties by reflection and handle them by it's type.

I need something like this:

Type t = obj.GetType();
PropertyInfo propInfo = t.GetProperty("article");
Type propType = ??  *GetPropType*() ??

switch (Type.GetTypeCode(propType))
{
  case TypeCode.Decimal:
    doSome1;
    break;
  case TypeCode.String:
    doSome2;
    break;
}

For strings it works to do propInfo.PropertyType.UnderlyingSystemType as GetPropType() but not for decimal for example.

For decimal works propInfo.PropertyType.GenericTypeArguments.FirstOrDefault(); but not for strings.

Hos can get the type for all simple types?

Thank you much. Nu





Aucun commentaire:

Enregistrer un commentaire