You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何获取系统数据类型列表以筛选自定义类型属性?

筛选类中的非系统类型属性

问题分析

你当前通过逐个判断类型的方式筛选非系统属性,这种方式扩展性差,新增系统类型时需要修改条件。可以通过预定义系统类型集合来简化判断逻辑,但注意你示例中的代码有误:systemTypeList.Contains(objPropertyInfo.Name)是判断属性名,而非属性类型,应该改为判断PropertyType。

解决方案

  1. 定义系统类型集合
    使用HashSet(Of Type)存储需要排除的系统类型,集合的Contains方法效率更高,适合频繁判断:
' 定义需要排除的系统类型集合
Dim systemTypeList As New HashSet(Of Type) From {
    GetType(Int32),
    GetType(String),
    GetType(Boolean),
    GetType(Decimal),
    GetType(Int64),
    GetType(Double),
    GetType(Date)
    ' 可根据需求添加更多系统类型
}
  1. 遍历属性并筛选
    遍历类的属性时,直接判断属性类型是否不在系统类型集合中:
Dim objProperties As PropertyInfo() = GetType(MyClassType).GetProperties()

For Each objPropertyInfo As PropertyInfo In objProperties
    ' 判断属性类型是否为非系统类型
    If Not systemTypeList.Contains(objPropertyInfo.PropertyType) Then
        ' 处理非系统类型属性的逻辑
        Console.WriteLine($"非系统类型属性:{objPropertyInfo.Name},类型:{objPropertyInfo.PropertyType.Name}")
    End If
Next

额外处理:可空值类型(Nullable)

如果你的类中包含可空系统类型(比如Nullable(Of Int32)),需要额外处理,通过Nullable.GetUnderlyingType获取其基础类型再判断:

For Each objPropertyInfo As PropertyInfo In objProperties
    Dim propertyType As Type = objPropertyInfo.PropertyType
    ' 处理可空类型,获取基础类型
    Dim underlyingType As Type = Nullable.GetUnderlyingType(propertyType)
    If underlyingType IsNot Nothing Then
        propertyType = underlyingType
    End If
    
    If Not systemTypeList.Contains(propertyType) Then
        ' 处理逻辑
    End If
Next

内容的提问来源于stack exchange,提问作者Md. Suman Kabir

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 14:35:18