Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Portable Class library and reflection

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.

like image 474
Rati_Ge Avatar asked Dec 27 '12 21:12

Rati_Ge


People also ask

What is portable class library?

The Portable Class Library project enables you to write and build managed assemblies that work on more than one . NET Framework platform. You can create classes that contain code you wish to share across many projects, such as shared business logic, and then reference those classes from different types of projects.

What is the difference between the portable class library and shared projects?

The difference between a shared project and a class library is that the latter is compiled and the unit of reuse is the assembly. Whereas with the former, the unit of reuse is the source code, and the shared code is incorporated into each assembly that references the shared project.

What is a class library used for?

A class library is a collection of books kept in the classroom and used for extensive reading, not generally for classroom activities. A classroom library can include readers, the teacher's own books, and books lent by learners.


1 Answers

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)
    {}
like image 147
Daniel Plaisted Avatar answered Oct 12 '22 05:10

Daniel Plaisted