Click or drag to resize

SFSArrayGetClass Method

Returns the element at the specified position as an instance of a custom class.

Namespace:  Sfs2X.Entities.Data
Assembly:  SmartFox2X (in SmartFox2X.dll) Version: (1.7.11)
public virtual Object GetClass(
	int index


Type: SystemInt32
The position of the element to return.

Return Value

Type: Object
The element of this array as a generic object type to be casted to the target class definition.


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 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.

The following example shows the same class on the client and server sides, which can be transferred back and forth with the GetClass(Int32) and AddClass(Object) methods.

The server-side Java definition of a SpaceShip class is:


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)
    { = name;
        this.type = type;

    // ... Getters / Setters ...

The client-side C# definition of the SpaceShip class is:

    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

        // ... Getters / Setters ...

A SpaceShip instance is sent by the server to the client in the first position of an array. This is how to retrieve it:

SpaceShip myShipData = (SpaceShip)sfsArray.GetClass(0);
See Also