Windows 8开发人员预览中缺少Type.GetProperty()方法

时间:2011-10-22 09:06:19

标签: c# reflection windows-8 windows-runtime microsoft-metro

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

更新

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

这个using System.Reflection;扩展方法需要

GetTypeInfo()

2 个答案:

答案 0 :(得分:24)

Metro的反思有所改变:见MSDN(“反思变化” - 靠近底部)。

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

答案 1 :(得分: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()就实现了所有平台。