C# 字体生成器设置字体大小

C# 字体生成器设置字体大小,c#,reflection,reflection.emit,C#,Reflection,Reflection.emit,我正在尝试构建一个动态类型并设置它的大小,比如[StructLayout(LayoutKind.Sequential,size=100)]can。我有以下代码: Type structLayoutAttr = typeof(StructLayoutAttribute); ConstructorInfo structLayoutAttrCtor = structLayoutAttr.GetConstructor(new[]{typeof(LayoutKind)}); object[] struct

我正在尝试构建一个动态类型并设置它的大小,比如
[StructLayout(LayoutKind.Sequential,size=100)]
can。我有以下代码:

Type structLayoutAttr = typeof(StructLayoutAttribute);
ConstructorInfo structLayoutAttrCtor = structLayoutAttr.GetConstructor(new[]{typeof(LayoutKind)});
object[] structLayoutAttrCtorArgs = new object[]{LayoutKind.Explicit};
FieldInfo[] structLayoutAttrSizeField = new FieldInfo[]{structLayoutAttr.GetField("Size")};
CustomAttributeBuilder cb = new CustomAttributeBuilder(structLayoutAttrCtor, structLayoutAttrCtorArgs, structLayoutAttrSizeField, new object[]{size});
tb.SetCustomAttribute(cb);
type = tb.CreateType();
但是,此代码在最后一步失败-
CreateType
抛出关于错误格式的
TypeLoadException
。有没有合适的方法应用该属性?

该属性是一个特殊的属性,由编译器处理并编译成特殊的元数据,因此您无法在自定义属性构建中应用它。检查,有一个采用该类型的大小