Returns the element corresponding to the specified key as an instance of a custom class.
Namespace:
Sfs2X.Entities.Data
Assembly:
SmartFox2X (in SmartFox2X.dll) Version: 1.8.0.0 (1.8.0)
Syntax public virtual Object GetClass(
string key
)
Parameters
- key
- Type: SystemString
The key whose associated value is to be returned.
Return Value
Type:
ObjectThe element of this object as a generic object type to be casted to the target class definition;
null if a mapping for the passed key doesn't exist.
Implements
ISFSObjectGetClass(String)Remarks
This advanced feature allows the transmission of specific object instances between client-side C# and server-side Java provided that:
- the respective class definitions on both sides have the same package/namespace name
- the class implements the SerializableSFSType interface on both sides
- the following code is executed right after creating the SmartFox object:
DefaultSFSDataSerializer.RunningAssembly = Assembly.GetExecutingAssembly(); (requires
System.Reflection and
Sfs2X.Protocol.Serialization)
IMPORTANT: class serialization is not supported in Unity WebGL.
Examples
The following example shows the same class on the client and server sides, which can be transferred back and forth with the
GetClass(String) and
PutClass(String, Object) methods.
The server-side Java definition of a SpaceShip class is:
package my.game.spacecombat
public class SpaceShip implements SerializableSFSType
{
private String type;
private String name;
private int firePower;
private int maxSpeed;
private List<String> weapons;
public SpaceShip(String name, String type)
{
this.name = name;
this.type = type;
}
}
The client-side C# definition of the SpaceShip class is:
namespace my.game.spacecombat
{
public class SpaceShip : SerializableSFSType
{
private string _type;
private string _name;
private int _firePower;
private int _maxSpeed;
private Array _weapons;
public SpaceShip(string name, string type)
{
_name = name
_type = type
}
}
}
The SpaceShip instance is sent by the server to the client. This is how to retrieve it:
SpaceShip myShipData = (SpaceShip)sfsObject.GetClass(key);
See Also