How to use TypeForwardedTo in portable class libraries?

I am trying to create a portable class library that uses implementations from the platform when it is available. For example, Lazy<T> is available in .NET 4.5, Windows Store Apps, Windows Phone 8, but it is not available on Windows Phone 7, Silverlight 4. When my PCL boots onto one of the platforms with the Lazy<T> implementation, I want to use the implementation platforms. When it is not available on the platform, I want to use my own implementation. This seems possible, because Microsoft BCL does it, but I did not understand how to implement it.

I read that with TypeForwardedToAttribute you can redirect PCL to use the implementation from the platform. I'm not quite sure how to set up Visual Studio projects to achieve this result. If CoreLib is my library and ShimLib contains my Lazy<T> implementation. Where can I add TypeForwardedToAttribute? The attribute requires an actual typeof(System.Lazy<>) type description typeof(System.Lazy<>) , which does not work if Windows Phone 7 is targeted in PCL. If I remove Windows Phone 7, I cannot add the link from CoreLib to ShimLib because ShimLib does not support all platforms that CoreLib does. How can I handle this?

Yes, I know that Lazy<T> very simple to implement, but this is just an example, and my actual situation applies to many other classes that are less trivial to implement.

+7
c # portable-class-library
source share
2 answers

The Microsoft.Bcl method does this by sending two assemblies with the same identifier; one with the type itself and one with the type forward. You are referring to a type targeting platforms that do not support Lazy (including portable library combinations that include one of these platforms). And refer to the one that has type-ahead when targeting platforms with Lazy, this allows you to use libraries created against older platforms.

Please note: Microsoft.Bcl has a slight advantage that you do not have. We send the assembly with the same name as in later versions, which means that when the Windows Phone 7 application runs on Windows Phone 8, they get the built-in version, and not the one that is in the strip library. You cannot imitate this, but it is probably something you can live with in your situation.

+8
source share

The principle idea of ​​type redirection is very well explained in this question and this , and I will not repeat the details here. However, the idea is to reuse library A without recompiling, although it refers to library B , which it replaces with library C To this end, library B must be modified so that it forwards a link to library C , which is what the TypeForwardedTo attribute does.

How will this help you? Well, you can create your ShimLib in such a way that all your projects reference it, but uses conditional compilation and type redirection to link it with frame libraries, if they exist. Fortunately, someone has already done that for you :)

Edit in response to Matt's comment: you're right, I forgot that Theraot is not returning to its original implementation. And I think it will be very difficult to achieve without recompiling. The best you can do is probably follow this strategy, that is, have different build configurations for different versions of the framework. This way you can conditionally compile TypeForwardedToAttribute . At the very least, this saves you from having to duplicate code. If you really do not want to distribute different versions of your code, you can implement a loader that detects the framework version and downloads the version of your assembly that was compiled using the assembly configuration for this version

+5
source share

All Articles