Search code examples
msbuildvisual-studio-2013portable-class-library

How to disable automatic selection of frameworks in PCL projects?


In a Portable Class Project in Visual Studio 2013, when I select

  • .NET Framework 4 and higher
  • Windows Store Apps (Windows 8) and higher
  • Windows Phone 8

Visual Studio claims that Silverlight 5 supports every available functionality that is portable between the other frameworks I selected and selects it automatically. But it's wrong because I need to use /unsafe compiler option which is not possible in Silverlight applications.

I select .NET Framework 4.5 instead of 4 as a workaround, so Visual Studio doesn't select Silverlight 5 automatically, but it shouldn't be needed since the project is perfectly compatible with .NET Framework 4 as well.

How can I get around that?
Is there anything I can change in the project file, like ProjectTypeGuids?


Solution

  • Well, you certainly can get around it. It just takes a bit of surgery on the .csproj file for the PCL project. Open it in a text editor, Notepad will do fine, and add this line to the <PropertyGroup>, after the ProjectTypeGuids property:

      <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
    

    Which is enough to fool MSBuild and the compiler to allow you to use unsafe code.

    I can't think of any failure scenario, you won't lose that property either when you make changes in the PCL project and rewrite the .csproj file. Your only nemesis could be a hyper-active Store validator that will reject your app, I consider the odds very low since unsafe code is otherwise fine. Just don't use it in a Silverlight project :)