How to use unsafe code on a Web Site

Asked

Viewed 150 times

5

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

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

Obviously the above code does not allow to use pointers, for this we need the keyword unsafe. I put her in the method, leaving the signature like this:

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

But I got the following message while compiling:

Unsafe code may only appear if compiling with /unsafe

When searching Google, the solution seemed simple: Just turn on the option Allow unsafe code on the Build tab inside the project properties, but I can’t find this option anywhere in my project, as shown in the image:

Build Website

Does anyone know if I’m doing anything wrong? Where is this option on VS2015? Or is it not possible on websites?

4 answers

4

If the application is an ASP.NET website

Add the tag compilers 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 file csproj), then on the last item (properties).

You will see a window like this below.

So just click on build and then tick the checkbox "Allow unsafe code".

properties page

  • Hello, I also saw similar responses to this in other places (OS in English/MSDN), but I don’t have this option in Website, only Web Application :/, I saw a people talking about putting some settings in the configuration JSON, but I don’t have this file either

  • ready @Arturotemplário

  • thank you very much! It now compiles, but the error message still continues, although it doesn’t look like any problem. You know what can be?

  • It’s an error message even it’s just a Warning (warning)?

  • Usually warnings are green underscores, right? This one is in red, which is really intriguing, because I’m used to red underscores not being compiled

4


Complementing the @jbueno 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

  • Hello, it worked perfectly, but the message "Unsafe code may only appear..." continues to appear, although the code compiles normally. Do you know if this can cause any problems?

  • Synchronized...

  • @What do you mean?

  • @Arturotemplário It was a comment for Mathias about "almost" posting at the same time the answer. It took me long to format by adding the <Configuration>.

  • complemented, ty @Ismael.

3

You have to do it in your hand by going through 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 suitable feature is to create the simplest application possible, in case it would be a console application.

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>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.