我正在将一个库项目迁移到.net标准,当我试图使用System.Reflection API调用Type:GetProperties()时,我得到了以下编译错误:
类型不包含“GetProperties”的定义
这是我的project.json
{
"version": "1.0.0-*",
"buildOptions": {
"debugType": "portable"
},
"dependencies": {},
"frameworks": {
"netstandard1.6": {
"dependencies": {
"NETStandard.Library": "1.6.0"
}
}
}
}我遗漏了什么?
发布于 2017-05-16 21:13:00
在编写这篇文章时,GetProperties()现在是:
typeof(Object).GetTypeInfo().DeclaredProperties;
发布于 2017-02-03 19:45:56
Update:随着.NET COre 2.0版本的发布,System.Type返回,因此这两个选项都可用:
typeof(Object).GetType().GetProperties()typeof(Object).GetTypeInfo().GetProperties()
这一项需要添加using System.Reflection;typeof(Object).GetTypeInfo().DeclaredProperties
注意,该属性返回的是IEnumerable<PropertyInfo>,而不是前两个方法的PropertyInfo[]。System.Type上大多数与反射相关的成员现在都在System.Reflection.TypeInfo上。
首先调用GetTypeInfo从Type获取TypeInfo实例
typeof(Object).GetTypeInfo().GetProperties();另外,不要忘记使用using System.Reflection;
https://stackoverflow.com/questions/42029808
复制相似问题