C
C#3y ago
scixing

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
DaVinki
DaVinki3y ago
Can you clarify
scixing
scixingOP3y ago
Type a = typeof(GameObject);
CreateGameObject<a>();
Type a = typeof(GameObject);
CreateGameObject<a>();
like this This cannot be compiled. I want to know if there is any way to realize this function without reflection
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
scixing
scixingOP3y ago
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 eyesbleed may need a lot of switch to do this. If I can, I don't want to do this
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
scixing
scixingOP3y ago
public static Type GetModelType(string name)
{
// get type
throw new NotImplementedException();
}
public static object CreateGameObject(GameObjectSave save)
{
string typename = save.TypeName;
Type type = GetModelType(typename);
return CreateGameObject<Unit>("some obj");
// CreateGameObject<type>("some obj");

}
public static Type GetModelType(string name)
{
// get type
throw new NotImplementedException();
}
public static object CreateGameObject(GameObjectSave save)
{
string typename = save.TypeName;
Type type = GetModelType(typename);
return CreateGameObject<Unit>("some obj");
// CreateGameObject<type>("some obj");

}
'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 bigthonk
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
scixing
scixingOP3y ago
Because some restrictions cannot be deserialized directly, it must be generated through the configuration file
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
scixing
scixingOP3y ago
Because it contains unity gameobject It's not important. I mainly want to know whether there is a similar way to do this
public static object CreateGameObject(GameObjectSave save)
{
string typename = save.TypeName;
Type type = GetModelType(typename);
var method = type.GetMethod("CreateGameObject").MakeGenericMethod(type);
return method.Invoke(null, new object[] {"dani", save});
// return CreateGameObject<Unit>("some obj");
// CreateGameObject<type>("some obj");

}
public static object CreateGameObject(GameObjectSave save)
{
string typename = save.TypeName;
Type type = GetModelType(typename);
var method = type.GetMethod("CreateGameObject").MakeGenericMethod(type);
return method.Invoke(null, new object[] {"dani", save});
// return CreateGameObject<Unit>("some obj");
// CreateGameObject<type>("some obj");

}
use reflection
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Thinker
Thinker3y ago
Yes, this is impossible without reflection, and you certainly don't want to use reflection in a game due to its major performance impact.
Up
Up3y ago
can't you simply use an interface?
IMyThing thing = HoweverYouLoadTheThing();
GameObject GameObj = thing.CreateGameObject();
IMyThing thing = HoweverYouLoadTheThing();
GameObject GameObj = thing.CreateGameObject();
scixing
scixingOP3y ago
This does not seem to solve the problem of initializing to the specified type(? In HowerYouLoadTheThing, I still encounter corresponding problems
Up
Up3y ago
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.
scixing
scixingOP3y ago
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 nervousowo is used in CreateGameObject<T>
Up
Up3y ago
one single dictionary lookup when deserializing, no other switches needed.
Accord
Accord3y ago
✅ This post has been marked as answered!
Want results from more Discord servers?
Add your server