How to use unsafe code in a Web Site

5

I was trying to practice the use of pointers in C #, so I created a website in the following method:

public static Nodo[] MontaTree(){ //... código ... }

Obviously the above code does not make it possible to use pointers, so we need keyword unsafe . I put it in the method, leaving the signature like this:

public static unsafe Nodo[] MontaTree(){ //... código ... }

But I received the following message when compiling:

  

Unsafe code may only appear if compiling with / unsafe

When searching for Google, the solution seemed simple: Just turn on the Allow unsafe code option on the Build tab within the project properties, but I can not find this option anywhere in my project, as the image shows:

Does anyone know if I'm doing something wrong? Where is this option on VS2015? Or is it not possible on WebSites?

    
asked by anonymous 10.04.2017 / 18:51

4 answers

4

Complementing @jbueno's response, when the project is not a web application, this option does not appear in the tab build in properties, try to put the following snippet in your web.config:

<configuration>
  ...
  <system.codedom>
      <compilers>
          <compiler 
              language="c#;cs;csharp" 
              extension=".cs" 
              compilerOptions="/unsafe"
              type="Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
      </compilers>
  </system.codedom>
</configuration>

source: how to add unsafe keyword

    
10.04.2017 / 19:20
4

If the application is an ASP.NET site

Add the compilers tag in the web.config file (within configuration -> system.codedom )

<configuration>
 <!-- outras tags -->

  <system.codedom>
    <compilers>
      <compiler language="c#;cs;csharp" extension=".cs" compilerOptions="/unsafe" type="Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
    </compilers>
  </system.codedom>
</configuration>

If it is a normal ASP.NET application

Right-click the project (in the csproj file), then the last item.

You will see a window like this one below.

Then just click build and then select the " Allow unsafe code " checkbox.

    
10.04.2017 / 19:15
3

You have to do it in hand by touching the file Web.config :

<compiler language="c#;cs;csharp" extension=".cs"
type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.3.0, Culture=neutral, PublicKeyToken=xxxxxxxxxxxx"
warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701 /unsafe+"/>

Anyway if it is to test the most appropriate feature is to create the simplest application possible, in case it would be a console application.

    
10.04.2017 / 19:24
1

Add in your Web.Config the following tag:

<configuration>
  ...
  <system.codedom>
    <compilers>
      <compiler
       language="c#;cs;csharp" extension=".cs"
       compilerOptions="/unsafe"
       type="Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
    </compilers>
  </system.codedom>
</configuration>
    
10.04.2017 / 19:24