Casting object instance to actual type

I send several different kinds of package classes, which I serialize and send over the network. All packages extend the AbstractPackage:

public abstract class AbstactPackage() {}

public class UpdatePackage : AbstractPackage {
    public float x, y, rot;

    public UpdatePackage(float pX, float pY, float pRot) {
        x = pX; y = pY; rot = pRot;
    }
}

After the package was received at the other computer it gets serialized into an abstractPackage instance and readResponse is called. But now I need to determine which type this instance is exactly (e.g. UpdatePackage)

private void readResponse(AbstractPackage p) {
    if(p is UpdatePackage) readResponse(p as UpdatePackage);
}

private void readResponse(UpdatePackage p) { ... }

At the moment I have 30 different if statement in the readResponse function for every kind of package I have. Is there a way I can do this more dynamically?

I cannot use the dynamic keyword because I am working with unity and it uses an old dotNet version.

Jon Skeet
people
quotationmark

You could use a dictionary from the type to a delegate to call when you receive a package of that type. So for example:

class PackageReader
{
    private static readonly Dictionary<Type, Action<AbstractPackage>> responseReaders;

    static PackageReader()
    {
        responseReaders = new Dictionary<Type, Delegate>();
        RegisterReader<UpdatePackage>(ReadUpdatePackage);
        RegisterReader<DownloadPackage>(ReadDownloadPackage);
        ...
    }

    private static void RegisterReader<T>(Action<T> reader)
    {
        Action<AbstractPackage> d = package => reader((T) package);
        responseReaders.Add(typeof(T), d);
    }

    private static void ReadResponse(AbstractPackage p)
    {
        responseReaders[p.GetType()].Invoke(p);
    }

    private static void ReadUpdatePackage(UpdatePackage p) { ... }
    private static void ReadDownloadPackage(DownloadPackage p) { ... }
    ...
}

(I've made each "read" method have a different name so that the method group conversion is clearly unambiguous.)

people

See more on this question at Stackoverflow