dimanche 31 janvier 2021

Can I get a generic template from string to input to a generic Method?

private void Message<T>(byte[] body) where T : someClass
{
    ...
}

I have this method, and I don't use the instance of T, I only need "T" which I can put that into another generic class. Also I have this switch statement :

                case SCPacket.Move:
                    Message<Corps.Move>(task.body);
                    break;
                case SCPacket.Push:
                    Pushed(task.body);
                    break;
                case SCPacket.ShortChat:
                    Message<Corps.ShortChat>(task.body);
                    break;
                case SCPacket.MediumChat:
                    Message<Corps.MediumChat>(task.body);
                    break;
                case SCPacket.LongChat:
                    Message<Corps.LongChat>(task.body);
                    break;

Actually What I'm trying to do is just kind of refactoring.

I guess I can get a "T" which I can put that into Message from enum(like above, I matched between element of enum and nested class's name.

I tried to use Type.GetType(), but in this case It returns variable that I cannot use in the generic template.

If do you have any idea coming up with, please let me know.





Aucun commentaire:

Enregistrer un commentaire