Rico Suter
Rico Suter

Reputation: 11868

Missing Type.GetProperty() method in Windows 8 Developer Preview

I'm trying to port a simple application to Windows 8 Metro (WinRT). It seems that some very basic methods are missing. One basic example: Type.GetProperty(). It is available for Windows Phone 7, Silverlight and .NET client profile. Do I have to install something (eg. a special library) or is this method simply not available in the .NET metro profile?

UPDATE

OK, thank you. Now I use this.GetType().GetTypeInfo().DeclaredProperties.

using System.Reflection; is needed to have this GetTypeInfo() extension method.

Upvotes: 23

Views: 6915

Answers (2)

redent84
redent84

Reputation: 19249

In addition to Nicholas Butler response, I ended up using this kind of extensions to maintain the code reusable in all platforms.

#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

This way, Type.GetProperty() is implemented for all platforms.

Upvotes: 12

Nick Butler
Nick Butler

Reputation: 24433

Reflection has changed a bit in Metro: see MSDN ( "Reflection changes" - near the bottom ).

Basically, you now need: type.GetTypeInfo().

Upvotes: 24

Related Questions