C#如何通过反射设置StructLayoutAttribute.Pack?

C#如何通过反射设置StructLayoutAttribute.Pack?,c#,reflection,struct,C#,Reflection,Struct,我正在通过反射动态创建一个C#struct,当我在调试器中检查该结构的类型时,我注意到默认值为8。我想把背包设置为1 从本质上讲,我想通过反射实现将此属性添加到结构声明中可以实现的功能: [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)] 我曾尝试在创建类型后使用反射,但由于StructLayoutAttribute属性没有Setter,因此会引发异常 我的直觉是,它需要在调用ModuleBuilder

我正在通过反射动态创建一个C#struct,当我在调试器中检查该结构的类型时,我注意到默认值为8。我想把背包设置为1

从本质上讲,我想通过反射实现将此属性添加到结构声明中可以实现的功能:

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
我曾尝试在创建类型后使用反射,但由于StructLayoutAttribute属性没有Setter,因此会引发异常

我的直觉是,它需要在调用ModuleBuilder.DefineType(我目前正在设置LayoutKind等)时发生,但我没有看到任何明显的对应

欢迎指点。请在此处填写该类的完整代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Reflection.Emit;

namespace AcmeCo.Serializable
{
    public class DynamicSimStructBuilder
    {

        private static Type structType;

        public static object GetStructInstance() {
            object ptInstance = Activator.CreateInstance(GetStruct(), new object[] { });
            return ptInstance;
        }

        public static Type GetStruct() {

            if (structType != null) {
               return structType; 
            }

            AppDomain myDomain = AppDomain.CurrentDomain;
            AssemblyName myAsmName = new AssemblyName("MyDynamicAssembly");

            AssemblyBuilder myAsmBuilder =
               myDomain.DefineDynamicAssembly(myAsmName, AssemblyBuilderAccess.RunAndSave);

            ModuleBuilder structModule = myAsmBuilder.DefineDynamicModule("StructModule", "MyDynamicAssembly.dll");

            TypeBuilder structTypeBld = structModule.DefineType("AcmeCo.ThirdPartyAPIWrapper.DyanmicStruct", TypeAttributes.Public |
                TypeAttributes.Sealed | TypeAttributes.SequentialLayout | 
                TypeAttributes.Serializable | TypeAttributes.AnsiClass, typeof(ValueType));

            // use a set of variables defined in an XML file to create Fields on the struct
            ThirdPartyAPIVariableCollection collection = ThirdPartyAPIVariableCollection.Deserialize();
            foreach (ThirdPartyAPIVariable variable in collection.ThirdPartyAPIVariables)
            {
                FieldBuilder field = structTypeBld.DefineField(variable.Name, Type.GetType(variable.SystemDataType, true) , FieldAttributes.Public);
            }

            // Base class and base class constructor.
            Type objType = Type.GetType("System.Object");
            ConstructorInfo objCtor = objType.GetConstructor(new Type[] { });

            Type[] ctorParams = { };

            ConstructorBuilder pointCtor =
               structTypeBld.DefineConstructor(MethodAttributes.Public,
                                              CallingConventions.Standard, ctorParams);
            ILGenerator ctorIL = pointCtor.GetILGenerator();

            // Build the constructor. Begin by invoking the base class
            // constructor. The zero-index parameter of the constructor
            // is the new instance. Store the values of the fields.
            ctorIL.Emit(OpCodes.Ldarg_0);
            ctorIL.Emit(OpCodes.Call, objCtor);
            ctorIL.Emit(OpCodes.Ret);


            // Create the type, and then create an instance of the type 
            // (or not, doesn't hurt to comment the CreateInstance line out...)
            Type ptType = structTypeBld.CreateType();

            object ptInstance = Activator.CreateInstance(ptType, new object[] { });

            DynamicSimStructBuilder.structType = ptType;
            int sizeOfNewData = System.Runtime.InteropServices.Marshal.SizeOf(ptType);
            Console.WriteLine("New type is: " + sizeOfNewData);

            // save the newly created type to a DLL for use later
            // (or not, doesn't hurt to comment the next line out...)
            myAsmBuilder.Save("MyDynamicAssembly.dll");

            return ptType;
        }
    }
}

看起来,再添加一个参数可以指定包装尺寸:

发件人:


啊,太好了。如果有一个由“计算机”组成的“网络”,我就可以在那里查找这些信息了。
public TypeBuilder DefineType(
    string name,
    TypeAttributes attr,
    Type parent,
    PackingSize packsize
)