Xamarin PCL中缺少Type.GetProperties()

use*_*922 18 c# xamarin.ios xamarin.android xamarin

在Xamarin PCL中,我正在尝试获取我编写的类的System.Reflection.PropertyInfo,以便我可以通过其字符串名称访问其属性来获取/设置,并且缺少Type.GetTypeInfo(),以及作为Type.GetProperties.但System.Reflection.PropertyInfo是一个有效的类.我怎样才能获得课程的属性信息?我是否必须为每个平台编写一个包装器?(它在Android/iOS项目中表现得很好).

use*_*922 32

这是一个扩展,所以你需要把它

using System.Reflection;
Run Code Online (Sandbox Code Playgroud)

在顶部.然后它可用:

        TypeInfo typeInfo = this.GetType().GetTypeInfo();
        foreach (PropertyInfo propInfo in typeInfo.DeclaredProperties)
Run Code Online (Sandbox Code Playgroud)

  • 也许这确实解决了你的特定情况下的问题,但``DeclaredProperties`不是`GetProperties()`的基础.这是因为`DeclaredProperties`不包含继承属性. (2认同)

Jam*_*rue 22

我刚刚碰到这个,很确定答案是使用:

Type.GetRuntimeProperties
Run Code Online (Sandbox Code Playgroud)