C
C#2y 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
DaVinki2y ago
Can you clarify
scixing
scixing2y 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 User2y ago
Message Not Public
Sign In & Join Server To View
scixing
scixing2y 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 User2y ago
Message Not Public
Sign In & Join Server To View
scixing
scixing2y 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 User2y ago
Message Not Public
Sign In & Join Server To View
scixing
scixing2y ago
Because some restrictions cannot be deserialized directly, it must be generated through the configuration file
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
scixing
scixing2y 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 User2y ago
Message Not Public
Sign In & Join Server To View
Thinker
Thinker2y 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
Up2y ago
can't you simply use an interface?
IMyThing thing = HoweverYouLoadTheThing();
GameObject GameObj = thing.CreateGameObject();
IMyThing thing = HoweverYouLoadTheThing();
GameObject GameObj = thing.CreateGameObject();
scixing
scixing2y ago
This does not seem to solve the problem of initializing to the specified type(? In HowerYouLoadTheThing, I still encounter corresponding problems
Up
Up2y 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
scixing2y 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
Up2y ago
one single dictionary lookup when deserializing, no other switches needed.
Accord
Accord2y ago
✅ This post has been marked as answered!