Skip to main content
Solved

How can I get the Field Type of a Field from Cache?


aaghaei
Captain II
Forum|alt.badge.img+10

Hello all,

I am wondering how can I get the Data Type (string, decimal, int ...) of a field. I have a method like below and I need to know what is the Data Type of the “Field”

 

public static void MyMethod<Field>(PXCache cache) where Field : IBqlField
{
    string fieldName = cache?.GetField(typeof(Field)); // This gets me the name correctly

    // I tried all below and they are returning null
    var aa = cache?.GetType()?.GetField(fieldName)?.FieldType;
    var bb = cache?.GetType()?.GetProperty(fieldName)?.PropertyType?.GetField(fieldName)?.FieldType;
    var cc = cache?.GetItemType()?.GetField(fieldName)?.FieldType;
    var dd = cache?.GetItemType()?.GetProperty(fieldName)?.PropertyType?.GetField(fieldName)?.FieldType;

    // Rest of my code
}

 

Best answer by Zoltan Febert

Hi @aaghaei,

Please try this code:

public static void MyMethod<Field>(PXCache cache) where Field : IBqlField
{
    var fieldName = cache.GetField(typeof(Field)); // This gets me the name correctly

    var propertyType = cache.GetItemType().GetProperty(fieldName)?.PropertyType;

    if (propertyType == null) return;

    var fieldType = propertyType.IsAssignableFrom(typeof(string)) ? typeof(string)
        : propertyType.IsAssignableFrom(typeof(int?)) ? typeof(int?)
        : propertyType.IsAssignableFrom(typeof(decimal?)) ? typeof(decimal?)
        : propertyType.IsAssignableFrom(typeof(bool?)) ? typeof(bool?)
        : typeof(object);
}

It worked for me for string, int? and decimal? types, I didn’t try for bool?, but it should work as well.

View original
Did this topic help you find an answer to your question?

4 replies

Zoltan Febert
Jr Varsity I
Forum|alt.badge.img+3

You can try this:

var typeCode = System.Type.GetTypeCode(cache.GetItemType().GetProperty(fieldName)?.PropertyType);

Type fieldType;
switch (typeCode)
{
    case TypeCode.Boolean:
        fieldType = typeof(bool);
        break;
    case TypeCode.Int32:
        fieldType = typeof(int?);
        break;
    case TypeCode.String:
        fieldType = typeof(string);
        break;
    case TypeCode.Empty:
    case TypeCode.Object:
    case TypeCode.DBNull:
    case TypeCode.Char:
    case TypeCode.SByte:
    case TypeCode.Byte:
    case TypeCode.Int16:
    case TypeCode.UInt16:
    case TypeCode.UInt32:
    case TypeCode.Int64:
    case TypeCode.UInt64:
    case TypeCode.Single:
    case TypeCode.Double:
    case TypeCode.Decimal:
    case TypeCode.DateTime:
    default:
        throw new ArgumentOutOfRangeException();
}

I have added all possible labels to the switch, you need to make them similar as the first three examples are.


aaghaei
Captain II
Forum|alt.badge.img+10
  • Author
  • Captain II
  • 1206 replies
  • March 13, 2024

Hi @Zoltan Febert 

Thank you for the response. Regardless of the data type of the “Field”, the GetTypeCode method always returns “Object” like this 

fieldType = BaseType = {Name = "Object" FullName = "System.Object"}


Zoltan Febert
Jr Varsity I
Forum|alt.badge.img+3
  • Jr Varsity I
  • 176 replies
  • Answer
  • March 13, 2024

Hi @aaghaei,

Please try this code:

public static void MyMethod<Field>(PXCache cache) where Field : IBqlField
{
    var fieldName = cache.GetField(typeof(Field)); // This gets me the name correctly

    var propertyType = cache.GetItemType().GetProperty(fieldName)?.PropertyType;

    if (propertyType == null) return;

    var fieldType = propertyType.IsAssignableFrom(typeof(string)) ? typeof(string)
        : propertyType.IsAssignableFrom(typeof(int?)) ? typeof(int?)
        : propertyType.IsAssignableFrom(typeof(decimal?)) ? typeof(decimal?)
        : propertyType.IsAssignableFrom(typeof(bool?)) ? typeof(bool?)
        : typeof(object);
}

It worked for me for string, int? and decimal? types, I didn’t try for bool?, but it should work as well.


aaghaei
Captain II
Forum|alt.badge.img+10
  • Author
  • Captain II
  • 1206 replies
  • March 14, 2024

@Zoltan Febert  Thank you very much. It works perfectly. Really appreciated.


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings