gpt4 book ai didi

c# - 在 C# 中,有没有一种方法可以使用反射仅检索内置数据类型属性

转载 作者:太空狗 更新时间:2023-10-30 00:17:14 25 4
gpt4 key购买 nike

使用反射我只想检索 built-in data type来自 C# 对象的属性。有没有比在 Where 方法中使用一堆 || (ors) 来指定我感兴趣的类型更好的方法?

Type sourceType = typeof(TSource);

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

最佳答案

它们都在 System 命名空间中,因此您至少可以过滤到命名空间,除此之外,至少列表不会太长。您也不会链接 Where's,您会使用 ||,该代码将不起作用。

关于c# - 在 C# 中,有没有一种方法可以使用反射仅检索内置数据类型属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3161959/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com