The Bait and Switch PCL Trick

Source: The Bait and Switch PCL Trick

I was sure I’ve already blog about it but, apparently, it’s not the case. This post is about a trick to use PCL: you provides a fake implementation in PCL and real implementations, using platform dependent code, for each supported platforms. Then you combine all this implementations in one nuget package.

Doing this way, this nuget package is usable in PCL but fake implementation will never be used. This trick is based on the fact that final application is never PCL and that nuget rules will provides the more specific variant.



From Thomas Levesque, a bunch of usefull string extension and a string format extended to fields and properties (use Regex to find named placeholder and Expression compilation to access it).

Supported syntax is less rich (no sub-property, no indexer) than DataBinder solution (like FormatWith 2.0 – String formatting with named variables) but it doesn’t require dependency on System.Web and can be PCL).

thomaslevesque/NString · GitHub.

Edit: We can refer to Fun With Named Formats, String Parsing, and Edge Cases and .Net ObjectFormatter – Using Tokens in a Format String for two other implementations and interesting discussion of how to escape brace and how to proceed. Basically, we have:

  1. to find {Xxxx}, either Regex or a hand coded parser using a small state machine,
  2. to evaluate Xxxx against an object instance :
    1. pure Reflexion to read field or property,
    2. Expression.Compile to read field or property,
    3. DataBinder.Eval with a rich syntax but slow and introducing a dependency on System.Web,
  3. to replace {Xxxx}, either Regex.Replace or using a generated standard String.Format with positional {2} or a manual construction.


Lots of helper extensions to format BCL types to more readable strings (and reverse).

Just like any other library, I have concrete cases of use of some extensions and some others seems useless. I dream of possibility to choose which subsetI take from a nuget library. Splitting in multiple parts is boring to use. This must be mitigated by the fact that binary is small: 76 Ko plus localisations assemblies.

Humanizer includes an evolution of the excellent ByteSize.

Humanizer by MehdiK.

Refit: Type-safe REST client

Refit: Type-safe REST client for:

  • Xamarin.Android
  • Xamarin.Mac
  • Desktop .NET 4.5
  • Windows Phone 8
  • Silverlight 5

and also packaged as PCL (obviously :)). This library use Castle.DynamicProxy to generate proxy implementation from attributed interface and is Task/IObservable aware from the start.

Even if it’s an elegant way of avoid a lot of plumbing code, it’s double-edged. We must be sure to have all the possibilities included and some are always missing:

  • Multipart requests
  • Form posts

Next platform wil be Xamarin.iOS and Windows Store (WinRT).

See paulcbetts/refit.