I dynamically build a struct for P/Invoke using
const TypeAttributes typeAttributes = TypeAttributes.Public |
TypeAttributes.SequentialLayout |
TypeAttributes.UnicodeClass;
var typeBuilder = moduleBuilder.DefineType("MyType", typeAttributes, typeof(ValueType));
after that, I construct the StructLayoutAttribute
and add it to the type like this
ConstructorInfo structLayoutAttributeConstructorInfo = typeof(StructLayoutAttribute).GetConstructor(new[] { typeof(LayoutKind) });
FieldInfo charSetFieldInfo = typeof(StructLayoutAttribute).GetField(nameof(StructLayoutAttribute.CharSet));
CustomAttributeBuilder attr = new CustomAttributeBuilder(structLayoutAttributeConstructorInfo,
new object[] { LayoutKind.Sequential },
new FieldInfo[] { charSetFieldInfo },
new object[] { CharSet.Unicode });
typeBuilder.SetCustomAttribute(structLayoutAttributeBuilder);
which is equivalent to setting
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
Now, the code works perfectly fine, regardless of whether or not I apply the StructLayoutAttribute
to the struct.
- What exactly is the difference between setting an explicit attribute and using the
TypeAttribute.SequentialLayout
flag?
Seemingly, setting the attribute is an uncessary redundancy, or am I missing something?