Hi all,
I would like to create an instance of a struct with
reflection.
The way I am used to doing this with classes:
- get a ConstructorInfo from the Type
- call Invoke()
However, if a struct does not have a constructor
declared, I don't get a single default constructor.
Here is a short code snippet that counts constructors:
using System;
using System.Reflection;
public class StructCreationWithReflection {
public static void Main(string[] args){
Type type = typeof(MyStruct);
ConstructorInfo[]cons = type.GetConstructors(
BindingFlags.Public |
BindingFlags.NonPublic |
BindingFlags.Instance
);
Console.WriteLine(cons.Length);
}
}
public struct MyStruct{
public int foo;
}
Is there any other possibility to create a struct, if I
only have it's type during runtime?
If I add a constructor to a struct, there are no problems
and I can use reflection to create instances perfectly,
just the same as if I was using a class.
Has the reflection feature simply been forgotten for
no-constructor structs?
Thanks for any hints.
Kind regards,
Carl
--
Carl Rosenberger
db4o - database for objects - http://www.db4o.com