在C#中,有没有办法只提取内置使用反射数据类型属性 [英] In C# is there a way retrieve only built-in data type properties using reflection

查看:104
本文介绍了在C#中,有没有办法只提取内置使用反射数据类型属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用反射,我想只检索的内建从C#对象数据类型属性。有没有更好的办法做到这一点,然后使用在其中,方法指定一堆 || 的(ORS) ?类型我感兴趣的是

 键入sourceType的= typeof运算(TSource); 

VAR道具= sourceType.GetProperties()
。凡(PI => pi.PropertyType == typeof运算(int)的
|| pi.PropertyType == typeof运算(字符串)); // ....等


解决方案

您看对于整型的BCL?或者只值类型? (IE整数,焦炭等)



如果是这样,你可以测试pi.PropertyType.IsPrimitive(),然后测试字符串类型作为或条款的一部分。 ..

  VAR道具= sourceType.GetProperties()
。凡(PI => .PropertyType.IsPrimitive
|| pi.PropertyType == typeof运算(字符串))


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) in a Where method specifying the types I am interested in?

Type sourceType = typeof(TSource);

var props = sourceType.GetProperties()
    .Where(pi => pi.PropertyType == typeof(int)
              || pi.PropertyType == typeof(string));    // .... etc.

解决方案

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))

这篇关于在C#中,有没有办法只提取内置使用反射数据类型属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆