在 .NET 核心中获取 class 的 public 属性
Getting the public properties of a class in .NET core
我注意到 .NET 核心不允许 myObj.GetType().GetProperties()
,因为不存在 GetProperties
方法。还有其他方法可以通过反射获取class的属性吗?
看来 myObj.GetType().GetProperties()
是有效的。我只需要在 using System.Reflection
之前引入 System.Reflection。
只是给其他人总结一下,仅仅在页面顶部添加 using System.Reflection
是不够的。您必须按照问题评论中的说明添加 System.Reflection.TypeExtensions
NuGet 包。
PM> Install-Package System.Reflection.TypeExtensions -Version 4.3.0
我注意到 .NET 核心不允许 myObj.GetType().GetProperties()
,因为不存在 GetProperties
方法。还有其他方法可以通过反射获取class的属性吗?
看来 myObj.GetType().GetProperties()
是有效的。我只需要在 using System.Reflection
之前引入 System.Reflection。
只是给其他人总结一下,仅仅在页面顶部添加 using System.Reflection
是不够的。您必须按照问题评论中的说明添加 System.Reflection.TypeExtensions
NuGet 包。
PM> Install-Package System.Reflection.TypeExtensions -Version 4.3.0