This question already has an answer here:
- C# using reflection to create a struct 3 answers
I'm trying to create an instance of a struct which's datatype is not known at compile time by obtaining and invoking it's parameterless constructor. The following (otherwise rather useless) code snippet shows what I do:
var i = new System.Int32();
var type = i.GetType();
var constructor = type.GetConstructor(System.Type.EmptyTypes);
var value = constructor.Invoke(null);
This doesn't work because type.GetConstructor(System.Type.EmptyTypes)
returns null
. When I step through this I can see that type
definitely is System.Int32
and as is evident from the first line there is a parameterless constructor. So why am I unable to obtain it?
I tried the same with an userdefined struct to rule out the posibility of restrictions in built in types but that shows the same behaviour.
Aucun commentaire:
Enregistrer un commentaire