In the typeattributes enumeration value of modulebuilder. definetype method, only class and interface are available. Where is struct? A struct is actually the sealed type inherited from valuetype.
This code:
Struct
{}
Will be compiled into the following Il:
. Class private sequential ANSI sealed beforefieldinit
Extends [mscorlib] system. valuetype
{}
Where sequential is equal to [structlayout (layoutkind. Sequential)]. (In this example, the struct does not define any members, so there will be sequential)
Beforefieldinit is not necessarily required to initialize the type after calling the static type method. This is also available by default. (If the type has a static constructor, this will disappear)
Sealed and Inherit System. valuetype are required. Therefore, creating a struct from modulebuilder requires at least the following steps:
// + Using system. Reflection
// + Using system. reflection. emit
Static void createstruct (modulebuilder MB)
{
Typebuilder = Mb. definetype ("mystruct", typeattributes. Sealed, typeof (valuetype ));
Typebuilder. createtype ();
}