获取对象基础类型 GetValueType
作者 : 管理员 发布于 2023-04-25 23:07:18 浏览 620 次
获取对象的基础类型

获取对象的基础类型,先看扩展方法,如下:

/// <summary>
/// 获取对象基础类型
/// </summary>
/// <param name="_">类型</param>
/// <returns></returns>
public static ValueTypes GetValueType(this Type _)

返回一个 ValueTypes 枚举类型 枚举类型值如下:

/// <summary>
/// 值类型枚举
/// </summary>
public enum ValueTypes
{
    /// <summary>
    /// 空
    /// </summary>
    [Description("空")] 
    Null = 0,
    /// <summary>
    /// 值
    /// </summary>
    [Description("值")] 
    Value = 1,
    /// <summary>
    /// 类
    /// </summary>
    [Description("类")] 
    Class = 2,
    /// <summary>
    /// 结构体
    /// </summary>
    [Description("结构体")] 
    Struct = 3,
    /// <summary>
    /// 枚举
    /// </summary>
    [Description("枚举")] 
    Enum = 4,
    /// <summary>
    /// 字符串
    /// </summary>
    [Description("字符串")] 
    String = 5,
    /// <summary>
    /// 数组
    /// </summary>
    [Description("数组")] 
    Array = 6,
    /// <summary>
    /// List
    /// </summary>
    [Description("List")] 
    List = 7,
    /// <summary>
    /// 字典
    /// </summary>
    [Description("字典")] 
    Dictionary = 8,
    /// <summary>
    /// ArrayList
    /// </summary>
    [Description("ArrayList")] 
    ArrayList = 9,
    /// <summary>
    /// 是否是集合类型
    /// </summary>
    [Description("是否是集合类型")] 
    IEnumerable = 10,
    /// <summary>
    /// 字典类型
    /// </summary>
    [Description("字典类型")] 
    IDictionary = 11,
    /// <summary>
    /// 匿名类型
    /// </summary>
    [Description("匿名类型")] 
    Anonymous = 12,
    /// <summary>
    /// DataTable
    /// </summary>
    [Description("DataTable")] 
    DataTable = 13,
    /// <summary>
    /// 其它
    /// </summary>
    [Description("其它")] 
    Other = 20
}

可以识别对象为以上类型,用法如下:

var a = "a".GetValueType();
var b = 10.GetValueType();
var c = new{a="a",b="b"}.GetValueType();
所有评论(0)