[C#] Script crash when using some code from package System

Hey guys, I really want to know what I’m missing to crash on this code, looks it’s missing some dotNet libraries. Need to include some specific DLL on files? The error isn’t just about Color, but there’s another functions which includes from package System and crashes too.

private static Task EntityControllerHandler() {
    Debug.WriteLine($"Color: {Color.Chartreuse}");
}

The error stack trace:

Exception thrown by a task: System.AggregateException: One or more errors occurred. ---> System.BadImageFormatException: Error verifying Saphirex.Example.Client.Modules.EntityManager:EntityControllerHandler (): Cannot load method from token 0x0a0000bc for call at 0x0167
  at CitizenFX.Core.BaseScript+<>c__DisplayClass40_1.<ScheduleTick>b__1 () [0x00049] in C:\gl\builds\master\fivem\code\client\clrcore\BaseScript.cs:176 
  at System.Threading.Tasks.Task`1[TResult].InnerInvoke () [0x0000f] in <4a74a468ceae4b8199f7400d96d8dff5>:0 
  at System.Threading.Tasks.Task.Execute () [0x00010] in <4a74a468ceae4b8199f7400d96d8dff5>:0 
   --- End of inner exception stack trace ---
---> (Inner Exception #0) System.BadImageFormatException: Error verifying Saphirex.Example.Client.Modules.EntityManager:EntityControllerHandler (): Cannot load method from token 0x0a0000bc for call at 0x0167
  at CitizenFX.Core.BaseScript+<>c__DisplayClass40_1.<ScheduleTick>b__1 () [0x00049] in C:\gl\builds\master\fivem\code\client\clrcore\BaseScript.cs:176 
  at System.Threading.Tasks.Task`1[TResult].InnerInvoke () [0x0000f] in <4a74a468ceae4b8199f7400d96d8dff5>:0 
  at System.Threading.Tasks.Task.Execute () [0x00010] in <4a74a468ceae4b8199f7400d96d8dff5>:0 <---
Failed to run a tick for Core: System.BadImageFormatException: Error verifying Saphirex.Example.Client.Modules.EntityManager:EntityControllerHandler (): Cannot load method from token 0x0a0000bc for call at 0x0167
  at CitizenFX.Core.BaseScript+<>c__DisplayClass40_1.<ScheduleTick>b__1 () [0x00049] in C:\gl\builds\master\fivem\code\client\clrcore\BaseScript.cs:176 
  at System.Threading.Tasks.Task`1[TResult].InnerInvoke () [0x0000f] in <4a74a468ceae4b8199f7400d96d8dff5>:0 
  at System.Threading.Tasks.Task.Execute () [0x00010] in <4a74a468ceae4b8199f7400d96d8dff5>:0 

Forgot to include this information can be useful for something, I’m using this settings on Client project:

<Project Sdk="Microsoft.NET.Sdk">

    <PropertyGroup>
        <TargetFramework>net48</TargetFramework>
        <DebugType>portable</DebugType>
        <TargetName>$(AssemblyName).net</TargetName>
        <DefineConstants>CLIENT</DefineConstants>
        <LangVersion>default</LangVersion>
        <RootNamespace>Saphirex.Example.Client</RootNamespace>
    </PropertyGroup>

    <ItemGroup>
        <ProjectReference Include="..\Shared\Shared.csproj" />
        <PackageReference Include="CitizenFX.Core.Client" Version="1.0.*" />
    </ItemGroup>

</Project>

While trying to solve this problem, I seem to have discovered another incompatibility, I also cannot use Vector3 nor the JSON Serializer. The JSON Serializer I think is because of the version of the .NET framework, FiveM must only use version 4 on the “Client” side.

I am here to report on my progress over the last two days. I’ve seen that many people put the following code in .csproj, but as far as I can see this doesn’t make any difference in FiveM. I have tried several things, but it seems to me that I am missing something simple.

  <ItemGroup>
    <Reference Include="Microsoft.CSharp" />
  </ItemGroup>

that code does not even seem to be able to be compiled

Hmmm, it seems that the problem is that my IDE is recognizing the latest version of C# or some extra configuration that makes it use the most updated framework version. I took a better look at the official FiveM nugget and saw that in the Client project, they are using a specific SDK and it has solved the problem and now gives me the correct functions and classes from the package System.

<Project Sdk="CitizenFX.Sdk.Client/0.2.3">

    <PropertyGroup>
        <TargetFramework>net452</TargetFramework>
        <DebugType>portable</DebugType>
        <TargetName>$(AssemblyName).net</TargetName>
        <DefineConstants>CLIENT</DefineConstants>
        <LangVersion>default</LangVersion>
        <RootNamespace>Saphirex.Example.Client</RootNamespace>
    </PropertyGroup>

    <ItemGroup>
        <ProjectReference Include="..\Shared\Shared.csproj" />
        <PackageReference Include="CitizenFX.Core.Client" Version="1.0.*" />
    </ItemGroup>

</Project>