1

I have a property defined as:

public bool[] _array { get; set; }                  
public bool?[] _null_array { get; set; }            

I followed the instructions in How do I determine the underlying type of an array

foreach (var _property in typeof(T).GetProperties())
{ 
    var _propertyType = _property.PropertyType;
    var _propertyName = _property.Name;

    var CheckArray = _propertyType.IsArray;
    var UType      = _propertyType.GetElementType().Name;
    ....
}

The results for UType is:

_array      => "Boolean"
_null_array => "Nullable`1"

How do I get the type of an array of nullable primitive ?

Thanks.

EBDS
  • 1,244
  • 5
  • 16
  • 1
    what do you mean by underlying type of an array of nullable primitive? – Vivek Nuna Jan 09 '23 at 06:26
  • @viveknuna eg. int[].... underlying type = int. I've removed underlying from the question. I think I might have mixed up with List... i was referring the link and it mentioned underlying. – EBDS Jan 09 '23 at 06:30

1 Answers1

1

You already have it. The array element type is bool? aka Nullable<bool> aka Nullable``1 (only one backtick, blame markdown) with generic argument bool. If you're after bool, then you'll want Nullable.GetUnderlyingType on the element type; this returns null for things that aren't Nullable<T>, so consider:

var type = _propertyType.GetElementType();
type = Nullable.GetUnderylingType(type) ?? type;
var UType = type.Name;
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • I've asked a related question https://stackoverflow.com/questions/75110741/how-to-get-the-properties-name-type-and-other-info-from-a-class-gotton-through. @MarcGravel – EBDS Jan 13 '23 at 14:58