Hashtable Deserialization & How to create a reference variable to a field obtained through reflection
I'm using the LLAPI to make my own custom networking system. Going pretty good. I use serialized hashtables to transfer RPC data. I serialize it without issue, but upon recieveing the serialized hashtable, I get an error trying to deserialize it. I looked at https://msdn.microsoft.com/en-us/library/system.runtime.serialization.formatters.binary.binaryformatter%28v=vs.110%29.aspx and I'm following the same criteria, but I get this error:
SerializationException: Unexpected binary element: 0 System.Runtime.Serialization.Formatters.Binary.ObjectReader.ReadObject (BinaryElement element, System.IO.BinaryReader reader, System.Int64& objectId, System.Object& value, System.Runtime.Serialization.SerializationInfo& info) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/ObjectReader.cs:254) System.Runtime.Serialization.Formatters.Binary.ObjectReader.ReadNextObject (BinaryElement element, System.IO.BinaryReader reader) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/ObjectReader.cs:130) System.Runtime.Serialization.Formatters.Binary.ObjectReader.ReadObjectGraph (BinaryElement elem, System.IO.BinaryReader reader, Boolean readHeaders, System.Object& result, System.Runtime.Remoting.Messaging.Header[]& headers) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/ObjectReader.cs:104) System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.NoCheckDeserialize (System.IO.Stream serializationStream, System.Runtime.Remoting.Messaging.HeaderHandler handler) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/BinaryFormatter.cs:179) System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize (System.IO.Stream serializationStream) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/BinaryFormatter.cs:136) NetworkMain.ReceiveMessages () (at Assets/NoobieNetworking/NetworkMain.cs:408) NetworkMain.Update () (at Assets/NoobieNetworking/NetworkMain.cs:123)
Here is my serialization (if this helps):
Hashtable data = new Hashtable();
data.Add( 0, "test" );
byte error;
byte[] buffer = new byte[1024];
Stream stream = new MemoryStream( buffer );
BinaryFormatter f = new BinaryFormatter();
f.Serialize( stream, data );
foreach ( int id in recipients ) {
NetworkTransport.Send( localSocket, id, channelReliable, buffer, ( int )stream.Position, out error );
LogNetworkError( error );
}
Here is my deserialization:
Stream stream = new MemoryStream( buffer );
BinaryFormatter f = new BinaryFormatter();
Hashtable tData = null;
tData = ( Hashtable )f.Deserialize( stream );//LINE 408
GameManager.gm.Log( "HASHTABLE DATA: " + tData[0] );
I've also tried sending an empty hashtable, I get the same error. FYI, if that helps
I have another question, and I dont want to wait 16 hours for moderator approval: Is it possible to set a reference variable to a field I obtained through reflection, given I know what game object contains that same class that the field is in?
For instance, theres a component (lets call it ScrComponent), and its on on gameObject (GameObj). That component in a C# class and contains the int32 field (intField). If I use reflection, like (like down below), can I create a reference variable to modify that variable directly as long as I still know what component it is a part of?
int obtainedInt = GameObj.GetComponent().GetField( "intField" ).GetValue( scrComponent );
I want to directly access the field with a reference variable so I dont have to keep using reflection. How can this be accomplished?