简体   繁体   English

如何获取可空基元数组的类型?

[英]How to get the Type of an Array of Nullable Primitive?

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: UType 的结果是:

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

How do I get the type of an array of nullable primitive?如何获取可空基元数组的类型?

Thanks.谢谢。

You already have it.你已经拥有了。 The array element type is bool?数组元素类型是bool? aka Nullable<bool> aka Nullable``1 (only one backtick, blame markdown) with generic argument bool . aka Nullable<bool> aka Nullable``1 (只有一个反引号,blame markdown),带有通用参数bool If you're after bool , then you'll want Nullable.GetUnderlyingType on the element type;如果你在bool之后,那么你会想要元素类型上的Nullable.GetUnderlyingType this returns null for things that aren't Nullable<T> , so consider:对于不是Nullable<T>的东西,这会返回null ,因此请考虑:

var type = _propertyType.GetElementType();
type = Nullable.GetUnderylingType(type) ?? type;
var UType = type.Name;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM