c# - 了解对象中的属性类型 C#

标签 c# reflection types properties

我知道如何使用反射获取对象属性:

var properties = typeof(T).GetProperties();

现在我怎么知道 properties[0] 是否是一个字符串?或者它可能是一个整数?我怎么知道?

最佳答案

properties 的每个元素都是一个 PropertyInfo ,它有一个 PropertyType属性,表示属性的类型。

例如,您可以使用:

if (properties[0].PropertyType == typeof(string))

或者如果您想以允许继承的方式检查某些内容:

if (typeof(Stream).IsAssignableFrom(properties[0].PropertyType))

关于c# - 了解对象中的属性类型 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6553390/

相关文章:

c# - 如何使用空参数 P/调用 SetFileTime?

c# - 使用 lambda 表达式创建带反射的对象

reflection - 我可以从 Kotlin 中函数类型的变量中获取 KFunction 吗?

function - PolyML 函数和类型

scala - 是否可以将类型参数的类型参数传播到 Scala 中的参数化类?

c# - 给定映射路径时 Workspace.DeleteMapping 方法抛出异常

c# - ASP.NET MVC4 : how to detect screen width on the filter Action?

c# - 获取 lambda 表达式的 MethodInfo

reflection - 使用reflect.Set设置字段

sql - 在 T-SQL 中有任何理由使用数字而不是 int 吗?