11
votes

I am building new application for Desktop, Windows 8 store and Windows phone at the same time. so I created Portable Class library to have common functionality across all platforms. my problem is that when I try to reuse my code inside PCL I can not access some methods and properties inside library. According to MSDN those methods are supported but I do know now why I can not access them.

        var property = memberExpression.Member as PropertyInfo;
        if (property == null)
        {
        }

        var getMethod = property.GetGetMethod(true);
        if (getMethod.IsStatic)
        {}

here is the fragment of the code that can not be compiled. GetGetMethod and IsStatic are in red inside Visual Studio editor. I have no idea why is that happening and how to access those properties.

so please if anyone out there has ever done something like that, help me to make this code compile.

2
What is the debugger sayig?Victor
GetMethod and GetGetMethod are tow different things so why should i use GetMethod? Compiler says that it can't resolve symbol GetGetMethod. This problem is if target platform is Windows Phone 8Rati_Ge
There have been some changes to the reflection API in .NET 4.5 for apps targeting either PCL or Windows Store. The following blog entry provides a nice walk-through of the changes: http://blogs.msdn.com/b/dotnet/archive/2012/08/28/evolving-the-reflection-api.aspx. Notably: GetMethod and siblings are no longer available. Use the DeclaredMethod(s) property instead (available on TypeInfo instances, which you can get from a type instance using .GetTypeInfo()).Morten Mertner
Scroll down to the Writing code for the new reflection API – Windows Store and Portable Class Library to find the information you need (but I'd recommend reading the whole blog post).Morten Mertner

2 Answers

18
votes

We did some refactoring in the reflection APIs for .NET for Windows Store apps. See the blog post Evolving the Reflection API for details. Among other things, the API changes set us up for better portability in the future. The new APIs are available in Windows Store apps, .NET 4.5 and Windows Phone 8. For compatibility, the old APIs are of course still available on .NET 4.5 and Windows Phone 8.

For Portable Class Libraries, if you target only platforms where the new reflection APIs are supported, then you will get only the new APIs. If you add a platform that doesn't support the new APIs then you will get the APIs.

PropertyInfo.GetGetMethod() isn't part of the new APIs, so you should use PropertyInfo.GetMethod instead. MethodInfo.IsStatic is part of the new APIs, the reason you saw red squiggles in Visual Studio there was because it didn't know what type getMethod was because you used var and GetGetMethod() wasn't recognized.

So, your code should look something like this:

    var property = memberExpression.Member as PropertyInfo;
    if (property == null)
    {
    }

    var getMethod = property.GetMethod;
    if (getMethod != null && getMethod.IsStatic)
    {}
0
votes

To provide some info about my comment use Type.GetMethod.

Metadata from Type

When evaluating metadata returned from System.Type methods and properties, the equality operator may return different results on different platforms. In the .NET Compact Framework, each successive call to the Type.GetMethod method returns a new instance of System.Reflection.MethodInfo, so return values are never evaluated as equal. However, in the .NET Framework 4, calling GetMethod with the same parameters returns the same instance of MethodInfo. You must review and potentially modify your code when you are working with the following System.Reflection types: MethodInfo, FieldInfo, PropertyInfo, EventInfo, MemberInfo, MethodBase, ConstructorInfo, and ParameterInfo.

Reflection on Open Generic Types

On Windows Phone 7, reflection is supported on closed generic types; however, most reflection operations are not supported on open generic types. On open generic types, only the following reflection operations are supported for Windows Phone 7: Retrieving an open generic type or method by calling the GetType or GetMethods method. Creating a generic type or method from an open generic type or method by calling the MakeGenericType or MakeGenericMethod method. For all other reflection operations on open generic types, a NotSupportedException exception is thrown.

Source: http://msdn.microsoft.com/en-us/library/gg597392.aspx