I tried to use yield return on my script and found that the script will crash with a System.MethodAccessException and traceback similar to this one:
[ 338859] [ GTAProcess] MainThrd/ Loaded CFXIteratorRepro.net, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null into ScriptDomain_6676278
[ 339250] [ GTAProcess] MainThrd/ Failed to instantiate instance of script CFXIteratorRepro.Repro: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.MethodAccessException: Error verifying CFXIteratorRepro.Repro/<get_TestIterator>d__1:.ctor (int): Method System.Environment:get_CurrentManagedThreadId () is not accessible at 0x000f
[ 339297] [ GTAProcess] MainThrd/ at CFXIteratorRepro.Repro.get_TestIterator () [0x00000] in <5f77149e57be412aa23804401b0ca9c2>:0
[ 339328] [ GTAProcess] MainThrd/ at CFXIteratorRepro.Repro..ctor () [0x00009] in D:\Proyectos\Grand Theft Auto V\CFXIteratorRepro\CFXIteratorRepro\Repro.cs:21
[ 339375] [ GTAProcess] MainThrd/ at (wrapper managed-to-native) System.Reflection.MonoCMethod:InternalInvoke (System.Reflection.MonoCMethod,object,object[],System.Exception&)
[ 339500] [ GTAProcess] MainThrd/ at System.Reflection.MonoCMethod.InternalInvoke (System.Object obj, System.Object[] parameters) [0x00002] in <74fbbe963b7e417b8d715b858c5c584f>:0
[ 339562] [ GTAProcess] MainThrd/ --- End of inner exception stack trace ---
[ 339641] [ GTAProcess] MainThrd/ at System.Reflection.MonoCMethod.InternalInvoke (System.Object obj, System.Object[] parameters) [0x00014] in <74fbbe963b7e417b8d715b858c5c584f>:0
[ 339703] [ GTAProcess] MainThrd/ at System.RuntimeType.CreateInstanceMono (System.Boolean nonPublic) [0x000a8] in <74fbbe963b7e417b8d715b858c5c584f>:0
[ 339750] [ GTAProcess] MainThrd/ at System.RuntimeType.CreateInstanceSlow (System.Boolean publicOnly, System.Boolean skipCheckThis, System.Boolean fillCache, System.Threading.StackCrawlMark& stackMark) [0x00009] in <74fbbe963b7e417b8d715b858c5c584f>:0
[ 339812] [ GTAProcess] MainThrd/ at System.RuntimeType.CreateInstanceDefaultCtor (System.Boolean publicOnly, System.Boolean skipCheckThis, System.Boolean fillCache, System.Threading.StackCrawlMark& stackMark) [0x00027] in <74fbbe963b7e417b8d715b858c5c584f>:0
[ 339875] [ GTAProcess] MainThrd/ at System.Activator.CreateInstance (System.Type type, System.Boolean nonPublic) [0x00020] in <74fbbe963b7e417b8d715b858c5c584f>:0
[ 339922] [ GTAProcess] MainThrd/ at System.Activator.CreateInstance (System.Type type) [0x00000] in <74fbbe963b7e417b8d715b858c5c584f>:0
[ 340031] [ GTAProcess] MainThrd/ at CitizenFX.Core.InternalManager.CreateAssemblyInternal (System.String assemblyFile, System.Byte[] assemblyData, System.Byte[] symbolData) [0x000b1] in C:\gl\builds\edf06b9b\0\cfx\fivem\code\client\clrcore\InternalManager.cs:136
The following code creates a Property that returns strings with yield return and tries to print them in the constructor, causing the traceback above:
using CitizenFX.Core;
using System.Collections.Generic;
namespace CFXIteratorRepro
{
public class Repro : BaseScript
{
public IEnumerable<string> TestIterator
{
get
{
for (int i = 0; i < 5; i++)
{
yield return $"String #{i}";
}
}
}
public Repro()
{
foreach (string str in TestIterator)
{
Debug.WriteLine(str);
}
}
}
}
I used the following fxmanifest.lua to test the script:
fx_version "bodacious"
game "gta5"
client_script "CFXIteratorRepro.net.dll"
And the following csproj was used to compile the script from Visual Studio 2019:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net452</TargetFramework>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
<AssemblyName>$(AssemblyName).net</AssemblyName>
<DefineConstants>FIVEM</DefineConstants>
<Version>1.0</Version>
<OutputPath>..\bin\$(Configuration)\</OutputPath>
<DebugType>embedded</DebugType>
<DebugSymbols>true</DebugSymbols>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="CitizenFX.Core.Client" Version="1.0.2669">
<IncludeAssets>compile</IncludeAssets>
</PackageReference>
<None Update="fxmanifest.lua">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>