23

我正在尝试将一个简单的应用程序移植到 Windows 8 Metro (WinRT)。似乎缺少一些非常基本的方法。一个基本的例子:Type.GetProperty(). 它适用于 Windows Phone 7、Silverlight 和 .NET 客户端配置文件。我是否必须安装一些东西(例如一个特殊的库)或者这种方法在 .NET Metro 配置文件中根本不可用?

更新

好的谢谢。现在我用this.GetType().GetTypeInfo().DeclaredProperties.

using System.Reflection;需要有这个GetTypeInfo()扩展方法。

4

2 回答 2

24

Metro 中的反射发生了一些变化:请参阅MSDN(“反射变化” - 靠近底部)。

基本上,你现在需要:type.GetTypeInfo().

于 2011-10-22T09:55:00.577 回答
12

除了 Nicholas Butler 的响应之外,我最终使用了这种扩展来维护代码在所有平台上的可重用性。

#if NETFX_CORE // Workaround for .Net for Windows Store not having Type.GetProperty method
    public static class GetPropertyHelper
    {
        public static PropertyInfo GetProperty(this Type type, string propertyName)
        {
            return type.GetTypeInfo().GetDeclaredProperty(propertyName);
        }
    }
#endif

这种方式Type.GetProperty()适用于所有平台。

于 2013-07-03T12:44:52.600 回答