Is there a way to call generic methods through Type (from typeof or GetType) (without reflection)
like
T Create<T>();
Type a = typeof(xxx);
18 Replies
Can you clarify
like this
This cannot be compiled. I want to know if there is any way to realize this function without reflection
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
Thank you. I want to create a new instance through the type name in the configuration file, but there seems to be no simple way to use generic may need a lot of switch to do this. If I can, I don't want to do this
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
'Type' is a runtime type. I wonder if there is any way to directly call generic methods through Type,
Well, it seems to violate the design idea of C#, which may be
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
Because some restrictions cannot be deserialized directly, it must be generated through the configuration file
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
Because it contains unity gameobject
It's not important. I mainly want to know whether there is a similar way to do this
use reflection
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
Yes, this is impossible without reflection, and you certainly don't want to use reflection in a game due to its major performance impact.
can't you simply use an interface?
This does not seem to solve the problem of initializing to the specified type(? In HowerYouLoadTheThing, I still encounter corresponding problems
well what you could do is a 2-step process, then:
have some kind of registry of object providers, mapped to a string ID.
then when loading, you read that string ID, retrieve the provider for that type, and feed it the rest of the file to turn it into an object.
This may lead to a lot of switch or other codes. If possible, I want to avoid them.
But it seems that I can only do this, or ignore performance and use is used in CreateGameObject<T>
one single dictionary lookup when deserializing, no other switches needed.
✅ This post has been marked as answered!