Why is my C# event handler failing argument mapping?

I’m building a C# resource and have a command for testing a scenario:

The handler looks like

I’m sendign a List and receiving List so why is the mapping failing when the handler is being invoked?

Hi,

I usually use the type “dynamic” when I want to listen to an event that has a complex parameters like a List or a Class that I have defined.

Example:

[EventHandler("resource:RecordStats")]
private void TestEvent([FromSource]Player source, dynamic stats)
{
     //do what you want, use the "dynamic" variable
    int count = stats.Count;
}

The downside of using the “dynamic” type is that autocomplete won’t work, because the dynamic type will resolve at runtime, so you will have to call methods on the object without the help of autocomplete.

Another solution might be to convert the object to json (using Newtonsoft.Json.dll), replacing the sending and listening type of the object to String , and then deserialize it on the listening event

Thanks. I dont’ seem to be able to cast the dynamic to List or pass to JsonConvert.

I don’t want to pass the argument as a member on another type. If I can’t cast then there’s issues.
image

I’ve never seen cast ing issues with dynamic types before.
Any ideas?

tl;dr you cannot send complex objects over events.


As @Federick said, you will need to use JSON if you wish to send complex objects to and from the server. Specifically, you’ll need to use Newtonsoft JSON Portable .NET 4.0 version 12.0.2.23222.

Ok, so is this a constraint of the FiveM messaging system or c#?
I just woudn’t have thought I’d get the compile time error, given I’ve been able to cast dynamics to pocos in other c# platforms. I would have imagined this to be more of a runtime error given what you’re saying. I’ve go this version of NewtonSoft in client and server so I’ll run that with.

<PackageReference Include="Newtonsoft.Json" Version="12.0.2" ExcludeAssets="Compile" GeneratePathProperty="true" />
<Reference Include="Newtonsoft.Json">
  <HintPath>$(PkgNewtonsoft_Json)\lib\portable-net40+sl5+win8+wp8+wpa81\Newtonsoft.Json.dll</HintPath>
</Reference>

As is said in the post I linked:

I have linked the relevant code below.

I still don’t understand why C# would have an issue “compiling”. This should surely be a runtime issue.

Ok, so granted we can’t pass a complex object, but the compile error I was talking about was that I needed to add the package Microsoft.CSharp.

Correct.

I do not have such as issue, perhaps it is something in your enviroment.

If it continues to cause you issues, you can try the Newtonsoft file and Reference I provided.