error CS0227: Unsafe code may only appear if compiling with /unsafe

This is the error message you get, when you build a project with some bit of unsafe code in it.

Simply you need to “Alt + Enter” on the project to see the properties and tick on “Allow unsafe code” on build tab. Then it compiles lovely in visual studio.

However, I came across the error message again in the build. with MSBuiild, I got the message. It’s still ticked on the project’s property page. What’s wrong? …

The culprit was build type. In VS, you usually set the build type to DEBUG. so the Allow unsafe code option is turned on against debug, not release. So, open the project file in a simple text editor. I used Sublime text 2. Make sure the option exists in both debug and release.

  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
    <DebugSymbols>true</DebugSymbols>
    <DebugType>full</DebugType>
    <Optimize>false</Optimize>
    <OutputPath>..\..\bin\Penge\</OutputPath>
    <DefineConstants>DEBUG;TRACE</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
    <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
    <DebugType>pdbonly</DebugType>
    <Optimize>true</Optimize>
    <OutputPath>bin\Release\</OutputPath>
    <DefineConstants>TRACE</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
    <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
  </PropertyGroup>
error CS0227: Unsafe code may only appear if compiling with /unsafe

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s