2011-10-22 77 views
23

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

UPDATE

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

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

+0

旁注:将现有的WP7应用程序移植到地铁并不那么简单。不仅有命名空间的变化......(Reflection,Streams,Dispatcher,...) –

回答

24

地铁中的反射已经发生了一些变化:请参见MSDN(“反射变化” - 接近底部)。

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

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()为所有平台上实现。

+0

当然,这是一个旧线程,但我想补充说GetDeclaredPropert(y/ies)只会返回当前类型的属性。这意味着如果Class2派生自Class1,则GetDerivedProperties将仅返回来自Class2的属性。 – Falgantil

+0

@BjarkeSøgaard如果你想搜索所有的属性,你可以使用'type.GetRuntimeProperty(propertyName)' – redent84

+0

其实我最终只是这样做的:for(; type!= null; type = type.GetTypeInfo()。BaseType) – Falgantil