【问题标题】:Reflection to get public instance properties in UWP在 UWP 中获取公共实例属性的反射
【发布时间】:2016-12-14 17:37:56
【问题描述】:

我正在将 Silverlight 应用程序移植到 UWP。 在我的 Silverlight 应用程序中,我获得了公共实例属性:

Type t;
t.GetProperties(BindingFlags.Instance | BindingFlags.Public);

在 UWP 中,GetProperties(BindingFlags.Instance | BindingFlags.Public) 方法似乎不再可用。在 UWP 中还有其他方法可以实现这一点吗?

谢谢。

【问题讨论】:

    标签: c# silverlight reflection uwp


    【解决方案1】:

    从此MSDN 来源,您可以执行以下操作:

    var props = t.GetTypeInfo().DeclaredProperties
                               .Where(x => x.GetMethod.IsPublic);
    

    【讨论】:

    • 感谢用户 3185569。如何指定绑定 flas 以获取非公共或非静态属性? foreach (PropertyInfo p in props ) { if (p.GetType().GetTypeInfo().IsPublic || p.GetType().GetTypeInfo().IsNotPublic )) || p.GetType().GetTypeInfo().IsInstance)//???? { //.... } }
    • @sarahberderian 你添加了using System.Reflection;,因为我可以在我的UWP 项目中使用GetProperties 和绑定标志。
    • @sarahberderian 但是,请查看更新后的答案以了解如何使用公共 getter 获取属性,如果您只想包含带有公共设置器的属性,请添加 || x.SetMethod.IsPublic
    • 感谢 user3185569 的帮助。当我创建一个新的 uwp 库项目时,我可以看到带有绑定标志的 t.GetProperties。然而,当将我原来的 silverlight 8.1 项目的目标更改为 ununiversal windows 10 时,我看不到 t.GetProperties!。
    • 那只会返回声明的属性。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-11
    • 1970-01-01
    • 2011-06-28
    • 2011-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多