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

Using reflection, I would like to get only the properties of the built-in data types from a C # object. Is there a better way to do this and then use a bunch of || (ors) in the Where method, which determines the types that interest me?

 Type sourceType = typeof(TSource); var props = sourceType.GetProperties() .Where(pi => pi.PropertyType == typeof(int) || pi.PropertyType == typeof(string)); // .... etc. 
+4
reflection c # lambda
Jul 01 2018-10-10T00:
source share
2 answers

Are you looking for integral types for BCL? Or just type values? (Integer IE, char, etc.)

If so, you can check for pi.PropertyType.IsPrimitive () and then check the type of the string as part of the sentence or ...

 var props = sourceType.GetProperties() .Where(pi => .PropertyType.IsPrimitive || pi.PropertyType == typeof(string)) 
+5
Jul 01 2018-10-10T00:
source share

They are all in the System namespace, so you can at least filter the namespace, except that at least the list is not too long. You would not mess with wherever you would use ||, this code will not work.

+6
Jul 01 2018-10-01T00:
source share



All Articles