In C# is there a way retrieve only built-in data type properties using reflection

后端 未结 2 537
花落未央
花落未央 2021-01-13 13:20

Using reflection I\'d like to retrieve only the built-in data type properties from a C# object. Is there a better way to do that then using a bunch of || (ors)

2条回答
  •  独厮守ぢ
    2021-01-13 13:51

    Are you looking for integral types to the BCL? Or value types only? (IE integer, char, etc)

    If so, you could test for pi.PropertyType.IsPrimitive() and then test for string type as part of the or clause...

    var props = sourceType.GetProperties()
        .Where(pi => .PropertyType.IsPrimitive
                  || pi.PropertyType == typeof(string))
    

提交回复
热议问题