I’m trying to serialize an object using System.Runtime.Serialization.Formatters.Binary.BinaryFormatter but I’m getting an exception related that the constructor of the BinaryFormatter is not accessible.
[ 73422] [ GTAProcess] MainThrd/ Error invoking callback for event onClientResourceStart: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.MethodAccessException: Error verifying FiveMGameMode.Client.MainScript:Serialize (object): Constructor System.Runtime.Serialization.Formatters.Binary.BinaryFormatter:.ctor () not visible from FiveMGameMode.Client.MainScript:Serialize (object) at 0x003c
[ 73485] [ GTAProcess] MainThrd/ at FiveMGameMode.Client.MainScript.OnStart (System.String resourceName) [0x00050] in <da9f11586ef547f184246bac619b9c8d>:0
[ 73563] [ GTAProcess] MainThrd/ at (wrapper managed-to-native) System.Reflection.MonoMethod:InternalInvoke (System.Reflection.MonoMethod,object,object[],System.Exception&)
[ 73610] [ GTAProcess] MainThrd/ at System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00032] in <74fbbe963b7e417b8d715b858c5c584f>:0
[ 73672] [ GTAProcess] MainThrd/ --- End of inner exception stack trace ---
[ 73719] [ GTAProcess] MainThrd/ at System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00048] in <74fbbe963b7e417b8d715b858c5c584f>:0
[ 73766] [ GTAProcess] MainThrd/ at System.Reflection.MethodBase.Invoke (System.Object obj, System.Object[] parameters) [0x00000] in <74fbbe963b7e417b8d715b858c5c584f>:0
[ 73828] [ GTAProcess] MainThrd/ at System.Delegate.DynamicInvokeImpl (System.Object[] args) [0x000e7] in <74fbbe963b7e417b8d715b858c5c584f>:0
[ 73906] [ GTAProcess] MainThrd/ at System.MulticastDelegate.DynamicInvokeImpl (System.Object[] args) [0x00008] in <74fbbe963b7e417b8d715b858c5c584f>:0
[ 73985] [ GTAProcess] MainThrd/ at System.Delegate.DynamicInvoke (System.Object[] args) [0x00000] in <74fbbe963b7e417b8d715b858c5c584f>:0
Code:
public byte[] Serialize(object serializableObject)
{
using (var memoryStream = new MemoryStream())
{
var formatter = new BinaryFormatter();
formatter.Serialize(memoryStream, serializableObject);
return memoryStream.ToArray();
}
}