代码之家  ›  专栏  ›  技术社区  ›  ahsteele tungi52

在C语言中,有没有一种方法使用反射只检索内置的数据类型属性

  •  4
  • ahsteele tungi52  · 技术社区  · 15 年前

    使用反射,我只想检索 built-in data type || (或)在 Where

    Type sourceType = typeof(TSource);
    
    var props = sourceType.GetProperties()
        .Where(pi => pi.PropertyType == typeof(int)
                  || pi.PropertyType == typeof(string));    // .... etc.
    
    2 回复  |  直到 15 年前
        1
  •  5
  •   ahsteele tungi52    15 年前

    你在找BCL的整数类型吗?还是仅值类型((整数、字符等)

    如果是这样,您可以测试pi.PropertyType.IsPrimitive(),然后作为or子句的一部分测试字符串类型。。。

    var props = sourceType.GetProperties()
        .Where(pi => .PropertyType.IsPrimitive
                  || pi.PropertyType == typeof(string))
    
        2
  •  6
  •   Richard Anthony Hein    15 年前

    它们都在系统名称空间中,因此您至少可以筛选到名称空间,除此之外,至少列表不会太长。你也不会链接Where's,你会使用| |,那代码不会工作。