Reflection: determining whether Type of field is signed or unsigned

  • Thread starter Thread starter Laszlo Szijarto
  • Start date Start date
L

Laszlo Szijarto

Using reflection, I am iterating through an array of FieldInfo objects and
wish to determine whether any given field represents a signed or an unsigned
variable (makes a difference in terms of how I parse some data from a raw
byte stream).

Is there anyway to determine whether a particular field is signed or
unsigned either from a FieldInfo object or else from a Type object stored in
fieldInfo.FieldType property?

Thank you very much for your help,
Laszlo
 
Laszlo,
Is there anyway to determine whether a particular field is signed or
unsigned either from a FieldInfo object or else from a Type object stored in
fieldInfo.FieldType property?

Since the set of integer types is limited, I guess the easiest way
would be to do

Type t = fieldInfo.FieldType:
if ( t == typeof(sbyte) || t == typeof(short) ||
t == typeof(int) || t == typeof(long) )
// it's signed
else if ( t == typeof(byte) || t == typeof(ushort) ||
t == typeof(uint) || t == typeof(ulong) )
// it's unsigned
else
// it's not an integer



Mattias
 
Thanks, Mattias,

that should work. Of course I was hoping for some kind of easy IsSigned
property.

Thank you,
Laszlo
 
Back
Top