[C#] [System.Reflection.TargetInvocationException] How to use the methods that return dynamics?

When I am tring to loop through player using the GetActivePlayers method I get an exception. I run this code in the construct of my Client script, but when I use it in the tick or some other method it always fails.

Code:

            List<object> players = GetActivePlayers();
            foreach (var player in players)
            {
                Debug.WriteLine(player.ToString());
            }

Exception:

[    451422] [    GTAProcess]             MainThrd/   at (wrapper managed-to-native) System.MonoCustomAttrs:GetCustomAttributesDataInternal (System.Reflection.ICustomAttributeProvider)
[    451422] [    GTAProcess]             MainThrd/   at System.MonoCustomAttrs.GetCustomAttributesData (System.Reflection.ICustomAttributeProvider obj) [0x0000e] in <74fbbe963b7e417b8d715b858c5c584f>:0 
[    451422] [    GTAProcess]             MainThrd/   at System.Reflection.CustomAttributeData.GetCustomAttributes (System.Reflection.Assembly target) [0x00000] in <74fbbe963b7e417b8d715b858c5c584f>:0 
[    451422] [    GTAProcess]             MainThrd/   at Mono.CSharp.ImportedAssemblyDefinition.ReadAttributes () [0x00000] in /mnt/idfk/meow/mono/mcs/mcs/import.cs:1719 
[    451422] [    GTAProcess]             MainThrd/   at Mono.CSharp.MetadataImporter.GetAssemblyDefinition (System.Reflection.Assembly assembly) [0x00027] in /mnt/idfk/meow/mono/mcs/mcs/import.cs:972 
[    451422] [    GTAProcess]             MainThrd/   at Mono.CSharp.ReflectionImporter.ImportAssembly (System.Reflection.Assembly assembly, Mono.CSharp.RootNamespace targetNamespace) [0x00000] in /mnt/idfk/meow/mono/mcs/mcs/reflection.cs:86 
[    451422] [    GTAProcess]             MainThrd/   at Microsoft.CSharp.RuntimeBinder.DynamicContext.Create () [0x000c6] in /mnt/idfk/meow/mono/mcs/class/Microsoft.CSharp/Microsoft.CSharp.RuntimeBinder/DynamicContext.cs:104 
[    451422] [    GTAProcess]             MainThrd/   at Microsoft.CSharp.RuntimeBinder.CSharpConvertBinder.FallbackConvert (System.Dynamic.DynamicMetaObject target, System.Dynamic.DynamicMetaObject errorSuggestion) [0x00000] in /mnt/idfk/meow/mono/mcs/class/Microsoft.CSharp/Microsoft.CSharp.RuntimeBinder/CSharpConvertBinder.cs:51 
[    451422] [    GTAProcess]             MainThrd/   at System.Dynamic.ConvertBinder.FallbackConvert (System.Dynamic.DynamicMetaObject target) [0x00000] in <31b946cfddcc4ab993dd6d559e89c25f>:0 
[    451422] [    GTAProcess]             MainThrd/   at System.Dynamic.DynamicMetaObject.BindConvert (System.Dynamic.ConvertBinder binder) [0x0000b] in <31b946cfddcc4ab993dd6d559e89c25f>:0 
[    451422] [    GTAProcess]             MainThrd/   at System.Dynamic.ConvertBinder.Bind (System.Dynamic.DynamicMetaObject target, System.Dynamic.DynamicMetaObject[] args) [0x00020] in <31b946cfddcc4ab993dd6d559e89c25f>:0 
[    451422] [    GTAProcess]             MainThrd/   at System.Dynamic.DynamicMetaObjectBinder.Bind (System.Object[] args, System.Collections.ObjectModel.ReadOnlyCollection`1[T] parameters, System.Linq.Expressions.LabelTarget returnLabel) [0x000c6] in <31b946cfddcc4ab993dd6d559e89c25f>:0 
[    451422] [    GTAProcess]             MainThrd/   at System.Runtime.CompilerServices.CallSiteBinder.BindCore[T] (System.Runtime.CompilerServices.CallSite`1[T] site, System.Object[] args) [0x00019] in <31b946cfddcc4ab993dd6d559e89c25f>:0 
[    451422] [    GTAProcess]             MainThrd/   at System.Runtime.CompilerServices.CallSiteOps.Bind[T] (System.Runtime.CompilerServices.CallSiteBinder binder, System.Runtime.CompilerServices.CallSite`1[T] site, System.Object[] args) [0x00000] in <31b946cfddcc4ab993dd6d559e89c25f>:0 
[    451422] [    GTAProcess]             MainThrd/   at (wrapper dynamic-method) System.Object:CallSite.Target (System.Runtime.CompilerServices.Closure,System.Runtime.CompilerServices.CallSite,object)
[    451422] [    GTAProcess]             MainThrd/   at Sander0542.FiveM.Games.Client.ClientMenu..ctor () [0x00032] in C:\GitHub\Sander0542\Sander0542.FiveM.Games\Client\ClientMenu.cs:23 
[    451422] [    GTAProcess]             MainThrd/   at (wrapper managed-to-native) System.Reflection.MonoCMethod:InternalInvoke (System.Reflection.MonoCMethod,object,object[],System.Exception&)
[    451422] [    GTAProcess]             MainThrd/   at System.Reflection.MonoCMethod.InternalInvoke (System.Object obj, System.Object[] parameters) [0x00002] in <74fbbe963b7e417b8d715b858c5c584f>:0 
[    451422] [    GTAProcess]             MainThrd/    --- End of inner exception stack trace ---
[    451422] [    GTAProcess]             MainThrd/   at System.Reflection.MonoCMethod.InternalInvoke (System.Object obj, System.Object[] parameters) [0x00014] in <74fbbe963b7e417b8d715b858c5c584f>:0 
[    451422] [    GTAProcess]             MainThrd/   at System.RuntimeType.CreateInstanceMono (System.Boolean nonPublic) [0x000a8] in <74fbbe963b7e417b8d715b858c5c584f>:0 
[    451422] [    GTAProcess]             MainThrd/   at System.RuntimeType.CreateInstanceSlow (System.Boolean publicOnly, System.Boolean skipCheckThis, System.Boolean fillCache, System.Threading.StackCrawlMark& stackMark) [0x00009] in <74fbbe963b7e417b8d715b858c5c584f>:0 
[    451422] [    GTAProcess]             MainThrd/   at System.RuntimeType.CreateInstanceDefaultCtor (System.Boolean publicOnly, System.Boolean skipCheckThis, System.Boolean fillCache, System.Threading.StackCrawlMark& stackMark) [0x00027] in <74fbbe963b7e417b8d715b858c5c584f>:0 
[    451422] [    GTAProcess]             MainThrd/   at System.Activator.CreateInstance (System.Type type, System.Boolean nonPublic) [0x00020] in <74fbbe963b7e417b8d715b858c5c584f>:0 
[    451422] [    GTAProcess]             MainThrd/   at System.Activator.CreateInstance (System.Type type) [0x00000] in <74fbbe963b7e417b8d715b858c5c584f>:0 
[    451422] [    GTAProcess]             MainThrd/   at CitizenFX.Core.InternalManager.CreateAssemblyInternal (System.String assemblyFile, System.Byte[] assemblyData, System.Byte[] symbolData) [0x000c4] in C:\gl\builds\master\fivem\code\client\clrcore\InternalManager.cs:150 ```

GetActivePlayers returns a list of ints(player sources). Not player objects
You can try to cast them to a player like this Player player = new Player(source). However dont know if this works on the client side.

You can also access all the players with the Players property accessable in the class that inherited the basescript.

You can also cast it to a player using that playerlist like ```Player player = new PlayerList[Source];

1 Like