I'm trying to create an instance of a generic type. I've tried getting the constructor through reflection, and now the way you'll see below, and both times the resulting object has been null.
Type type = typeof(StatDefinition<>).MakeGenericType(statTypes[selectedType]);
object definitionObject = Activator.CreateInstance(type, newName, newDescription);
StatTopic.WriteLine(definitionObject);
To help understand the variables, statTypes[selectedType] is just a basic type (bool, int, float, etc.). For my testing, it has always been typeof(float). 'newName' and 'newDescription' are both strings.
The constructor for StatDefinition<> is:
public StatDefinition(string name, string description) : base(name, description)
{
StatTopic.WriteLine(typeof(T));
}
My console is first outputting 'System.Single' (from the constructor of StatDefinition), following by 'null' (definitionObject is null).
I cannot seem to find the reason for this, the constructor is obviously being called without issue, yet the new object isn't being returned.
Aucun commentaire:
Enregistrer un commentaire