【发布时间】:2015-10-12 05:11:31
【问题描述】:
我遇到了结合MarshalAs 属性的反射问题。我想在运行时动态创建带有反射的结构。结构可能包含需要编组的数组。 Field.SetMarshal() 方法自 .NET 2.0 以来已过时,我找不到替代方法。
我在运行时构建的结构示例:
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
public struct Example
{
public int var1;
public float var2;
public byte var3;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
public int[] var4;
}
使用反射构建结构的代码:
TypeBuilder tb = mb.DefineType(pT.name, // Name of struct type
TypeAttributes.Public | // Public scope
TypeAttributes.SequentialLayout | // Sequential layout
TypeAttributes.AnsiClass, // Ansi Charset
typeof(ValueType), // Value type
PackingSize.Size1); // Packing size is 1 byte
// Add public fields to new struct type
foreach (parsedField pF in pT.fields)
{
FieldBuilder fb = tb.DefineField(pF.name, pF.type, FieldAttributes.Public);
// Add Marshall information to arrays
if (fb.FieldType.IsArray)
{
// ADD MARSHAL INFORMATION HERE
}
}
在过时的 UnmanagedMarshal 类的文档中,微软说:“Emit the MarshalAs custom attribute instead”
好吧,我尝试创建一个CustomAttribute(“MarshalAs”)并将其添加到Field.SetCustomAttribute(),但这也不起作用 - 结构的大小总是错误的。
[AttributeUsage(AttributeTargets.Field)]
public class MarshalAs : System.Attribute
{
public UnmanagedType type;
public int SizeConst;
public MarshalAs(UnmanagedType type, int SizeConst)
{
this.type = type;
this.SizeConst = SizeConst;
}
}
...
TypeBuilder tb = mb.DefineType(pT.name, // Name of struct type
TypeAttributes.Public | // Public scope
TypeAttributes.SequentialLayout | // Sequential layout
TypeAttributes.AnsiClass, // Ansi Charset
typeof(ValueType), // Value type
PackingSize.Size1); // Packing size is 1 byte
// Add public fields to new struct type
foreach (parsedField pF in pT.fields)
{
FieldBuilder fb = tb.DefineField(pF.name, pF.type, FieldAttributes.Public);
// Add Marshall information to arrays
if (fb.FieldType.IsArray)
{
ConstructorInfo ci = typeof(MarshalAs).GetConstructor(new Type[] { typeof(UnmanagedType), typeof(int) });
CustomAttributeBuilder customBuilder = new CustomAttributeBuilder(ci, new object[] { UnmanagedType.ByValArray, 8 });
fb.SetCustomAttribute(customBuilder);
}
}
【问题讨论】:
标签: .net dynamic reflection struct marshalling